简体   繁体   English

更新基于PHP会话的购物车中的数量

[英]Update quantity In PHP Session Based Shopping Cart

Hello and a happy new year to everyone! 大家好,新年快乐! I work with php sessions...I have a cart...I want to update the quantity via session... Here is my update action within the main file 我使用php会话...我有一个购物车...我想通过会话更新数量...这是我在主文件中的更新操作

if(isset($_POST["update"]))
    { 
        if ($_SESSION["item_id"] = $_GET["id"])
             { 
          $_SESSION['quantity'] = $_POST['quantity'];
             }
    }

And here is the form within the foreach loop to display the checked product 这是foreach循环中的表单,用于显示已检查的产品

  $total = 0;
     foreach ($_SESSION['cart'] as $item) {
    ?>
    <tr>
    <td><?php echo $item['name']; ?></td>
    <td>
    <form method="post" action="index2.php?action=update&id=<?php echo 
     $item["item_id"]; ?>">

     <input type="text" name="quantity" value="1" class="form-control" />
    <input type="submit" name="update" style="margin-top:5px;" class="btn btn-success" value="update" />
              </form>
<?php   echo  $_SESSION['quantity'];  //here I try to dislpay the new quantity that the user sets
 echo $item["item_id"]; ?></td>

Problem 问题

It changes the quantity in all the products within the basket and not in the specific one I want. 它会更改购物篮中所有产品的数量,而不更改我想要的特定产品中的数量。

Please forgive me because I am a new bye. 请原谅我,因为我是再见。

I added one more dimension to the $_SESSION array, using the item ID: 我使用商品ID向$ _SESSION数组添加了另一个维度:

$_SESSION['item'][$_GET['id']]['quantity'] = $_POST['quantity'];

Then when updating the quantity in the form: 然后在更新表格中的数量时:

echo $_SESSION['item'][$item['item_id']]['quantity'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM