首页 > 解决方案 > 数量未在会话中更新

问题描述

我正在使用我兄弟的帐户,因为我的帐户由于某些问题而无法使用。

我正在做一个电子商务网站。我的担忧与数量有关。

我有三个页面Test1.phpTest2.phpTest3.php一个 AJAX 页面,称为ajax_cart.php. 它正在处理 ADD TO CART with SESSION

让我们谈谈Test1.php到目前为止我在数据库中只有 4 个产品的页面,并且我只显示数量以用于测试目的。我的数量将从 1 开始。用户可以使用加号(+)和减号(-)增加或减少数量。

在此处输入图像描述

现在我做了什么,我将产品的数量从 1 增加到 4,然后单击添加到购物车按钮。它在顶部显示正确的数量,如“ 4 view cart”。

在此处输入图像描述

页面没有问题Test1.php。我点击view cart然后页面重定向Test2.php

让我们谈谈Test2.php页面

在此页面中,我只显示quantityand proceed to check out。它只是为了测试。我在这个页面上得到了正确的数量。

在此处输入图像描述

现在我做了什么,我将Test2.php页面中的数量从 4 增加到 6(任何东西) 在此处输入图像描述

然后点击Proceed to checkout页面重定向到 Test3.php 页面但我得到我的数量只显示4。为什么是4?因为 SESSION 但我的新数量是 6。

所以我的问题是,我增加了 中的数量,Test2.php但我的数量没有反映在Test3.php页面中,因为SESSION.

我正在与您分享 google 驱动器链接,因为我无法在此处上传我的代码,因为它很大。

https://drive.google.com/file/d/1KfJbjXlzwVIIX6IW9UKQYWHNKJ84P-3G/view?usp=sharing

你能帮我解决这个问题吗?

ajax_cart.php

<?php
session_start();
include('connection.php');
 $action = $_POST['action'];
 $p_id   = $_POST['p_id'];

if (isset($_POST['quantity'])) {
         echo $quantity = $_POST['quantity'];
}

if($action == 'add'){
     if(!empty($p_id)){
           $query = "SELECT p_id, p_images, p_name, p_brandname, p_company, p_packing, p_oldprice, p_currentprice FROM products WHERE p_id=?";
            if ($stmt = $conn->prepare($query)) {
                $stmt->bind_param("i", $p_id);
                $stmt->execute();
                $stmt->bind_result($p_id,$p_images, $name, $brandname, $company, $packing, $oldprice, $currentprice);
                $stmt->fetch();
              }
             $product = array(
                "p_id"=>$p_id,
                "p_brandname"=>$brandname,
                "p_currentprice"=>$currentprice,
                "p_total"=>$currentprice*$quantity,
                "p_images"=>$p_images,
                "quantity"=>$quantity
             );
            // print_r($product);
        if(isset($_SESSION['product_cart']) && !empty($_SESSION['product_cart']))
        {
            if(!array_key_exists($p_id,$_SESSION['product_cart']))
            {
                $_SESSION['product_cart'][$p_id] = $product;
            }
            else{

                     $_SESSION['product_cart'][$p_id]['p_total'] += ($currentprice*$quantity);
                     $_SESSION['product_cart'][$p_id]['quantity'] += $quantity;
            }       
        }
        else{
          $_SESSION['product_cart'][$p_id] = $product;
        }
    }   
}
?>
<a class="text-center" href="test2.php">View Cart</a>

测试2.php

<?php 
session_start();
include('connection.php');
 ?>

<!doctype html>
<html lang="en">
<head>    
      <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

                <!-- table section ends here -->

                    <?php if(!empty($_SESSION['product_cart'])){?>
                    <form action="test3.php" method="post">
                    <?php  foreach($_SESSION['product_cart'] as $key=>$product):?>
                      <div class="product-snipet">
                                <div class="sp-quantity">
                                    <div class="sp-minus fff"><a class="eee" href="javascript:void(0)" data-multi="-1">-</a></div>
                                    <div class="sp-input">
                                        <input type="text" class="quntity-input" value="<?php echo $product['quantity'];?>" />
                                    </div>
                                    <div class="sp-plus fff"><a class="eee" href="javascript:void(0)" data-multi="1">+</a></div>
                                </div>
                              </div>
                            <?php endforeach;?>
                            <input type="submit" name="submit" class="active" value="Proceed to checkout">
                     </form>
                    <?php }else{echo "<h2 style='font-size:22px'>Cart is empty</h2>";}
                    ?>


    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script type="text/javascript">
        /*increase the product qty*/
  updateTotal();
$('a.eee').click(function() {

  var $productContainer = $(this).closest('div.sp-quantity');
  var $pro_list = $(this).closest('tr.pro-list');
  var productPrice = parseFloat($pro_list.find('span.price_cal').text());
  var $quantityInput = $productContainer.find('input.quntity-input');
  var newQuantity = parseFloat($quantityInput.val()) + parseFloat($(this).data('multi'));

  if (newQuantity >= 1) {
    // Refresh quantity input.
    $quantityInput.val(newQuantity);

    // Refresh total div.
    var lineTotal = productPrice * newQuantity;
    $pro_list.find('td.total_amount').html('&#36;' + lineTotal);
    $pro_list.find('td.total_amount').data('price', lineTotal); //update data-price
  }
  updateTotal();
});

function updateTotal() {
  var subTotal = 0;
  var currencySymbol = "$";
  //start getting the total amounts from each product row.
  //add them as a subtotal.
  $("tr.pro-list > td.total_amount").each(function(index, element) {
    subTotal += parseFloat($(element).data("price")); //more secure to use data!
  });

  var total = subTotal + $("tr.pro-list.ship > td[data-price]").data("price");
  $("tr.pro-list.sub > td.subtotal").html(currencySymbol + "" + subTotal);
  $("tr.pro-list.total > td.total").html(currencySymbol + "" + total);
}

    </script>
</body>
</html>

测试3.php

 <?php 
     if(!empty($_SESSION['product_cart'])): foreach($_SESSION['product_cart'] as $key=>$product): ?>
       <b class="circle-qty"><?php echo $product['quantity'];?></b>
       <input type="hidden" name="o_product_qty[]" value="<?php echo $product['quantity'];?>"> 
       <?php endforeach;?>
       <?php endif;?> 

标签: javascriptphpjqueryhtmlsession

解决方案


这是因为您没有在会话中保存数据。您必须像在“添加购物车”中一样保存它。以下是我这边的两个选项:

  1. 您必须使用 AJAX 将更改保存到会话。就像您在 Test1.php 上所做的那样。您必须为 Plus 或 Minus 操作提供onclick使用调用ajax_cart.php来保存更改。add_cart(p_id="")
  2. 通过您现在正在进行的提交后保存对 Test3.php 中的会话的更改。

测试2.php

<?php 
session_start();
include('connection.php');
 ?>

<!doctype html>
<html lang="en">
<head>    
      <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

                <!-- table section ends here -->

                    <?php if(!empty($_SESSION['product_cart'])){?>
                    <form action="test3.php" method="post">
                    <?php  foreach($_SESSION['product_cart'] as $key=>$product):?>
                      <div class="product-snipet">
                                <div class="sp-quantity">
                                    <div class="sp-minus fff"><a class="eee" href="javascript:void(0)" data-multi="-1">-</a></div>
                                    <div class="sp-input">
                                        <input type="text" class="quntity-input" name="quantity[]" value="<?php echo $product['quantity'];?>" />
										<input type="hidden" name="p_id[]" value="<?php echo $key;?>" />
                                    </div>
                                    <div class="sp-plus fff"><a class="eee" href="javascript:void(0)" data-multi="1">+</a></div>
                                </div>
                              </div>
                            <?php endforeach;?>
                            <input type="submit" name="submit" class="active" value="Proceed to checkout">
                     </form>
                    <?php }else{echo "<h2 style='font-size:22px'>Cart is empty</h2>";}
                    ?>
         

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script type="text/javascript">
        /*increase the product qty*/
  updateTotal();
$('a.eee').click(function() {

  var $productContainer = $(this).closest('div.sp-quantity');
  var $pro_list = $(this).closest('tr.pro-list');
  var productPrice = parseFloat($pro_list.find('span.price_cal').text());
  var $quantityInput = $productContainer.find('input.quntity-input');
  var newQuantity = parseFloat($quantityInput.val()) + parseFloat($(this).data('multi'));

  if (newQuantity >= 1) {
    // Refresh quantity input.
    $quantityInput.val(newQuantity);

    // Refresh total div.
    var lineTotal = productPrice * newQuantity;
    $pro_list.find('td.total_amount').html('&#36;' + lineTotal);
    $pro_list.find('td.total_amount').data('price', lineTotal); //update data-price
  }
  updateTotal();
});

function updateTotal() {
  var subTotal = 0;
  var currencySymbol = "$";
  //start getting the total amounts from each product row.
  //add them as a subtotal.
  $("tr.pro-list > td.total_amount").each(function(index, element) {
    subTotal += parseFloat($(element).data("price")); //more secure to use data!
  });

  var total = subTotal + $("tr.pro-list.ship > td[data-price]").data("price");
  $("tr.pro-list.sub > td.subtotal").html(currencySymbol + "" + subTotal);
  $("tr.pro-list.total > td.total").html(currencySymbol + "" + total);
}
 
	</script>
</body>
</html>

测试3.php

<?php 
session_start();
include('connection.php');

if (isset($_POST['quantity']) && isset($_POST['p_id'])) {
	foreach($_POST['p_id'] as $key => $val) {
		if(isset($_SESSION['product_cart']) && isset($_SESSION['product_cart'][$val]))
		{
			$p_id	=	$val;
			$quantity	=	$_POST['quantity'][$key];
			$currentprice	=	$_SESSION['product_cart'][$p_id]['p_currentprice'];

			$_SESSION['product_cart'][$p_id]['p_total'] = ($currentprice*$quantity);
			$_SESSION['product_cart'][$p_id]['quantity'] = $quantity;
		}
	}
}
?>
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	 <?php 
         if(!empty($_SESSION['product_cart'])): foreach($_SESSION['product_cart'] as $key=>$product): ?>
           <b class="circle-qty"><?php echo $product['quantity'];?></b>
           <input type="hidden" name="o_product_qty[]" value="<?php echo $product['quantity'];?>"> 
           <?php endforeach;?>
           <?php endif;?> 

</body>
</html>


推荐阅读