更新购物车数量而不更新其他产品

Update shopping cart quantity without update other products

我正在研究如何在不更新其他产品的情况下更新我的购物车。我应该在这里使用会话吗?我当前的问题是,每当我更改数量时,它也会更新其他产品并将框中的数量设置回 1。我该如何更改它?

这就是我目前拥有的,我明白为什么它会更新所有产品,但我不知道该怎么做。

<?php
session_start();

include("header.php");
include_once("dbconnect.php");
include("functies.php");

if (empty($_SESSION['cart'])) {
    echo "U heeft geen producten in uw winkelwagen";

} else { 

$items = $_SESSION['cart'];
$cartitems = explode(",", $items);

?> 

<div align="center">
<?php titel(); ?>
<h1 Winkelwagen <h1>
</div>

<div class="container">
    <div class="row">
        <div class="col-sm-12 col-md-10 col-md-offset-1">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Quantity</th>
                        <th class="text-center">Price</th>
                        <th class="text-center">Total</th>
                        <th> </th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $total = 0;
                        $i=1;

                        foreach ($cartitems as $key=>$id) { 
                            $sql = "SELECT * FROM products WHERE id = $id";
                            $res=mysqli_query($conn, $sql);
                            $r = mysqli_fetch_assoc($res);

                            $sqlb = "SELECT * FROM brewery WHERE code = $r[brewery_code]";
                            $resb=mysqli_query($conn, $sqlb);
                            $rb = mysqli_fetch_assoc($resb);

                            if (isset($_POST['submit'])) {
                                $amount = $_POST['amount'];
                            } else $amount = 1;
                        ?>  
                    <tr>
                        <td class="col-sm-8 col-md-6">
                        <div class="media">
                            <img class="thumbnail pull-left" src="Images/<?php echo $r['name'] ?>.jpg" width="85" height="152" alt="..."  >
                            <div class="media-body">
                                <h4 class="media-heading"><?php echo $r['name']; ?></h4>
                                <h5 class="media-heading"> by <?php echo $rb['name'];; ?></a></h5>
                                <span>Status: </span><span class="text-success"><strong>In Stock</strong></span>
                            </div>
                        </div></td>
                        <td class="col-sm-1 col-md-1" style="text-align: center">
                        <form action="" method="post">
                        <input type="number" class="form-control" name="amount" value="1" min="1">
                        <input type="submit" name="submit" class="btn btn-primary btn-sm">
                        </form>
                        </td>
                        <?php 
                             $total = $total + $r['price'];
                             $i++;  
                             $producttotal = $amount * $r['price'];
                            ?>
                        <td class="col-sm-1 col-md-1 text-center"><strong>€ <?php echo number_format($r['price'],2);?> </strong></td>
                        <td class="col-sm-1 col-md-1 text-center"><strong>€ <?php echo number_format($producttotal,2);?> </strong></td>
                        <td class="col-sm-1 col-md-1">
                        <button type="button" class="btn btn-danger">
                            <a href="delcart.php?remove=<?php echo $key; ?>">Verwijderen</a>
                        </button></td>
                    </tr>

                        <?php } ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td>   </td>
                        <td>   </td>
                        <td>   </td>
                        <td><h5>Subtotal<br>Estimated shipping</h5><h3>Total</h3></td>
                        <td class="text-right"><h5><strong>.59<br>.94</strong></h5><h3>.53</h3></td>
                    </tr>
                    <tr>
                        <td>   </td>
                        <td>   </td>
                        <td>   </td>
                        <td>
                        <button type="button" class="btn btn-primary"> Continue Shopping </button></td>
                        <td>
                        <button type="button" class="btn btn-primary"> Checkout  </button>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>

                        <?php } ?>

您的表格需要包含您要增加哪个产品数量的指示器。例如,像这样:

 <form action="" method="post">
       <input type="number" class="form-control" name="amount[<?php echo $id;?>]" value="1" min="1">
       <input type="submit" name="submit" class="btn btn-primary btn-sm">
 </form>

然后您可以像这样评估 ID:

if (isset($_POST['submit']) && isset($_POST['amount'][$id])) {
    $amount = $_POST['amount'][$id];
} else { 
    $amount = 1;
}

我不明白你为什么要将金额设置为 1,如果它没有在 $_POST 中设置的话。我认为您必须在会话中存储每个产品的数量,而不仅仅是您现在所做的 ID。

可能是这样的:

   if (isset($_POST['submit']) && isset($_POST['amount'][$id])) {
        $amount = intval($_POST['amount'][$id]);
    } else { 
        if(isset($_SESSION['amounts'][$id])) {
            $amount = $_SESSION['amounts'][$id];
        } else { 
            $amount = 1;
        }
    }
    if(!isset($_SESSION['amounts'])) $_SESSION['amounts'] = array();
    $_SESSION['amounts'][$id] = $amount;