I am stuck in this error for 2 days, please help me out on this.
Detailed Query:
I am able to add the items into the cart using PHP Session but why Another session variable showing in the HTML Table after login.
insert_cart.php
<?php
session_start();
$prod_name= $_POST['prod_name'];
$price= $_POST['price'];
$qty= $_POST['qty'];
$id= $_POST['id'];
$prod_img= $_POST['prod_img'];
$product = array($prod_name,$price,$qty,$id,$prod_img);
$_SESSION[$prod_name] = $product;
//print_r($product);
header('location:view_cart.php');
?>
view_cart.php
<div class="container pt-1 pb-1">
<div class="row">
<div class='table-responsive'>
<table class='table table-condensed table-striped table-hover'>
<thead>
<tr>
<th>Product</th>
<th>Price:</th>
<th>Quantity</th>
<th>Total Prices</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<?php
$bill= 0;
$sno = 1;
//print_r($_SESSION);
foreach($_SESSION as $products){
//print_r($products);
echo "<tr>";
echo "<form action='edit_cart.php' method='post'>";
if (!$products) $products = array();
foreach($products as $key =>$value){
if($key == 0){
echo "<input type='hidden' name='name$key' class='form-control' value='".$value."'>";
echo "<td>".$value."</td>";
} else if($key == 1){
$p = $value;
echo "<input type='hidden' name='name$key' class='form-control' value='".$value."'>";
echo "<td>".$value."</td>";
}else if($key == 2){
$q = $value;
echo "<td><input type='number' name='name$key' class='form-control col-xl-4 text-center' min='1' value='".$value."'></td>";
$bill = ($p * $q);
echo "<td>".($bill)."</td>";
echo "<td><input type='submit' name='event' value='Update' class='btn btn-sm btn-warning'></td>";
echo "<td><input type='submit' name='event' value='Delete' class='btn btn-sm btn-danger'></td>";
}
}
echo "</form>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
?>
Output in View_Cart.php:
But if I do log in I am getting this error.
and line number 57 is : foreach($products as $key =>$value){
var_dump($products); Output,
Why login_exec.php $_SESSION['SESS_FIRST_NAME'] -> Santhosh Gururaj showing here.
<?php
session_start();
require_once('connection.php');
$errmsg_arr = array();
$errflag = false;
$username =$_POST['user_name'];
$password =$_POST['password'];
if($username == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location:login.php");
exit();
}
$sth = $conn->prepare("SELECT * FROM users WHERE email='$username' and password='$password' and status='active'");
$sth->execute();
if ($sth->rowCount() >= 1 ) {
$user = $sth->fetch(PDO::FETCH_ASSOC);
session_regenerate_id();
$_SESSION['SESS_MEMBER_ID'] = $user['id'];
$_SESSION['SESS_FIRST_NAME'] = $user['name'];
$_SESSION['SESS_LAST_NAME'] = $user['password'];
$_SESSION['msg']="You Are Loggedin Succesfully!";
session_write_close();
header("location: view_cart.php");
exit();
}else {
//Login failed
$errmsg_arr[] = 'User name and Password not found';
$errflag = true;
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
$_SESSION['msg']="You Are Loggedin Succesfully!";
header("location:login.php");
exit();
}
}
{
die("Query failed");
}
?>
During login you're setting$_SESSION['SESS_MEMBER_ID']
and other use data. The errors you see are because you're trying to loop trough these values as well, which aren't arrays, so they can't be looped trough.
A solution would be to add a separate'products'
key to your session ininsert_cart.php
, e.g.:
<?php
$prod_name = filter_input(INPUT_POST, 'prod_name', FILTER_SANITIZE_STRING);
$something = filter_input(...);
$product = array($prod_name, $something, ...);
$_SESSION['products'][$prod_name] = $product;
Then inview_cart.php
you can loop trough the products:
$products = $_SESSION['products'] ? (array)$_SESSION['products'] : [];
foreach($products as $product) {
// ...
}
Aside from that, please keep in mind: You can never trust user input!
Your current set-up allows me to overwrite the entire session, if I'd post$_POST['prod_name'] = 'SESS_MEMBER_ID';
, I could possibly switch to another user, or at least mangle the session.
Hava a look at the filter_input functions for more info. Same goes for way you're using mysqli_prepare, use bind param instead of embedding the variables into the query.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.