Php 如何取消设置会话项目计数编号

Php 如何取消设置会话项目计数编号,php,e-commerce,session-variables,Php,E Commerce,Session Variables,请帮助,我正在练习建立一个电子商务网站,我希望在他们成功订购产品后,购物车图标中的商品计数将返回到0并停止所有会话。假设我订购了3个产品,那么购物车的商品标识为3,完成后返回0。它确实返回到0,但当您单击加载到load_Cart.php的购物车图标时,它仍将加载到我订购的上一个项目 从checkout\u action.php>>>>转到标题(“位置:order\u confirmation.php?checkout=success”) 我使用这个load_cart.php加载购物车 <

请帮助,我正在练习建立一个电子商务网站,我希望在他们成功订购产品后,购物车图标中的商品计数将返回到0并停止所有会话。假设我订购了3个产品,那么购物车的商品标识为3,完成后返回0。它确实返回到0,但当您单击加载到load_Cart.php的购物车图标时,它仍将加载到我订购的上一个项目

checkout\u action.php>>>>转到标题(“位置:order\u confirmation.php?checkout=success”)

我使用这个load_cart.php加载购物车

 <?php
 session_start();
 include("includes/head.php");
 include("includes/db_config.php");
 include("includes/jsbottom.php");

 $data = "<table class='table table-striped table-responsive'>
   <thead>
     <tr>
       <th>Product</th>
       <th>Price</th>
       <th>Quantity</th>
       <th>Sub-Total</th>
       <th>Action</th>
       </tr>
     </thead>
   <tbody>";

$grand_total = 0;
foreach($_SESSION['cart'] as $product_id => $quantity) {
$sql = "SELECT * FROM items where id = '$product_id' ";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
   while($row = mysqli_fetch_assoc($result)) {
     $product_name = $row["product_name"];
     $product_desc = $row["product_desc"];
     $price = $row["price"];
     $image = $row["image"];

     //For computing the sub total and grand total
     $subTotal = $quantity * $price;
     $grand_total += $subTotal;

     $data .="<tr>
             <td>$product_name . <div class='middle'> <img 
            src='$image' style='height:100px;width:100px;'/> </div> </td>
            <td id='price$product_id'> $price</td>
            <td><input type='number' class ='form-control' 
           value = '$quantity' id='quantity$product_id' 
           onchange='changeNoItems($product_id)' min='1' size='5'></td>
           <td id='subTotal$product_id'>$subTotal</td>
           <td><button class='btn btn-danger' 
           onclick='removeFromCart($product_id)'><span class='glyphicon 
           glyphicon-trash' aria-hidden='true'></span> Remove</button></td>
           </tr>";
         }
       }
     }

    $data .="</tbody></table>
      <hr>
      <a href='checkout.php'> <h3 align='right'>Total: &#x20B1; <span 
          id='grandTotal'>$grand_total </span><br><button class='btn btn- 
          success btn-lg'><span class='glyphicon glyphicon-ok-sign'></span> 
          Check Out</button></h3>  </a>
          <hr>";

   echo $data;
?>

您的代码没有检查
$\u SESSION['item\u count']
是否为0。它正在获取
$\u会话['cart']
的内容。您需要检查
$\u会话['item\u count']
是否为0,或者销毁
$\u会话['cart']
,例如:

<?php session_start();
  if ($_GET['checkout'] =='success') {
  session_destroy();
 }
?>


您可以在未设置的情况下添加$\u会话['item\u count']=0($\u会话['item\u count']);签出成功后,请检查您尝试取消设置会话变量的
if条件。可能是它没有在
中获得成功值,如果
这就是它失败的原因。我想知道是否所有会话(如登录会话)也将被销毁是会话\u销毁将销毁所有会话信息。如果要重置购物车会话,则应使用unset($会话['cart'])取消设置$会话['cart']
 <?php
 session_start();
 include("includes/head.php");
 include("includes/db_config.php");
 include("includes/jsbottom.php");

 $data = "<table class='table table-striped table-responsive'>
   <thead>
     <tr>
       <th>Product</th>
       <th>Price</th>
       <th>Quantity</th>
       <th>Sub-Total</th>
       <th>Action</th>
       </tr>
     </thead>
   <tbody>";

$grand_total = 0;
foreach($_SESSION['cart'] as $product_id => $quantity) {
$sql = "SELECT * FROM items where id = '$product_id' ";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0) {
   while($row = mysqli_fetch_assoc($result)) {
     $product_name = $row["product_name"];
     $product_desc = $row["product_desc"];
     $price = $row["price"];
     $image = $row["image"];

     //For computing the sub total and grand total
     $subTotal = $quantity * $price;
     $grand_total += $subTotal;

     $data .="<tr>
             <td>$product_name . <div class='middle'> <img 
            src='$image' style='height:100px;width:100px;'/> </div> </td>
            <td id='price$product_id'> $price</td>
            <td><input type='number' class ='form-control' 
           value = '$quantity' id='quantity$product_id' 
           onchange='changeNoItems($product_id)' min='1' size='5'></td>
           <td id='subTotal$product_id'>$subTotal</td>
           <td><button class='btn btn-danger' 
           onclick='removeFromCart($product_id)'><span class='glyphicon 
           glyphicon-trash' aria-hidden='true'></span> Remove</button></td>
           </tr>";
         }
       }
     }

    $data .="</tbody></table>
      <hr>
      <a href='checkout.php'> <h3 align='right'>Total: &#x20B1; <span 
          id='grandTotal'>$grand_total </span><br><button class='btn btn- 
          success btn-lg'><span class='glyphicon glyphicon-ok-sign'></span> 
          Check Out</button></h3>  </a>
          <hr>";

   echo $data;
?>
<?php session_start();
  if ($_GET['checkout'] =='success') {
  session_destroy();
 }
?>