Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP会话数组访问另一个会话_Php_Mysql_Session Variables - Fatal编程技术网

PHP会话数组访问另一个会话

PHP会话数组访问另一个会话,php,mysql,session-variables,Php,Mysql,Session Variables,我陷入这个错误已经两天了,请帮我解决这个问题 详细查询: 我可以使用PHP会话将项目添加到购物车中,但是为什么登录后HTML表中会显示另一个会话变量呢 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']; $pr

我陷入这个错误已经两天了,请帮我解决这个问题

详细查询:

我可以使用PHP会话将项目添加到购物车中,但是为什么登录后HTML表中会显示另一个会话变量呢

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>";         
?>

产品
价格:
量
总价
更新
删除

在登录期间,您将设置
$\u会话['SESS\u成员\u ID']
和其他使用数据。您看到的错误是因为您试图通过这些值进行循环,而这些值不是数组,因此无法通过这些值进行循环

解决方案是在
insert_cart.php
中为您的会话添加一个单独的
'products'
键,例如:


非常感谢您的回复。。我会试试你应该有一个数组。也就是说,将所有产品保留在
$\u SESSION['products']
中,我建议可能更好的键是产品id,假设它比产品名称更可能是唯一的。所以:
if(!isset($_SESSION['PRODUCTS']){$_SESSION['PRODUCTS']=array();}$_SESSION['PRODUCTS'][id]=$product;
我需要在insert_Cart.php中替换此代码,知道吗,先生?良好的代码缩进将帮助我们阅读代码,更重要的是,它将帮助您调试代码,为您自己的利益着想。您可能会被要求在几周/几个月内修改此代码,最后您将感谢我。是的,在insert_Cart.php中。非常感谢您
<?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");
          
   }
   ?>