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

Php 更新购物车数量而不更新其他产品,php,mysql,shopping-cart,Php,Mysql,Shopping Cart,我正在尝试解决如何在不更新其他产品的情况下更新我的购物车。我是否应该在这里处理会话?我目前的问题是,每当我更改数量时,它也会更新其他产品,并将框中的数量设置回1。我该如何改变这一点 这就是我目前拥有的,我理解为什么它会更新所有的产品,但我不知道如何做,否则 <?php session_start(); include("header.php"); include_once("dbconnect.php"); include("functies.php"); if (empty($_SE

我正在尝试解决如何在不更新其他产品的情况下更新我的购物车。我是否应该在这里处理会话?我目前的问题是,每当我更改数量时,它也会更新其他产品,并将框中的数量设置回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>$24.59<br>$6.94</strong></h5><h3>$31.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 } ?>

.jpg“width=“85”height=“152”alt=“…”>
通过
状态:库存中


  
  
  
小计
预计发货总额 24.59美元
6.94美元
31.53美元       继续购物 结账
您的表单需要包含一个指标,表明您希望增加哪种产品的数量。例如:

 <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>
if (isset($_POST['submit']) && isset($_POST['amount'][$id])) {
    $amount = $_POST['amount'][$id];
} else { 
    $amount = 1;
}
   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;
我不明白,如果没有在$\u POST中设置金额,为什么要将其设置为1。我认为您必须在会话中存储每个产品的金额,而不是像现在这样仅存储ID

也许是这样:

 <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>
if (isset($_POST['submit']) && isset($_POST['amount'][$id])) {
    $amount = $_POST['amount'][$id];
} else { 
    $amount = 1;
}
   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;