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

标签 php mysql shopping-cart

我正在尝试弄清楚如何在不更新其他产品的情况下更新我的购物车。我是否应该在这里进行 session ?我当前的问题是,每当我更改数量时,它也会更新其他产品,并将框中的数量设置回 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 } ?>

最佳答案

您的表单需要包含您想要增加哪种产品数量的指示符。例如,像这样:

 <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;
}

我不明白如果 $_POST 中没有设置金额,为什么要将金额设置为 1。我认为您必须在 session 中存储每个产品的金额,而不仅仅是像您现在所做的那样。

也许像这样:

   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;

关于php - 更新购物车数量而不更新其他产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47157204/

相关文章:

php - 通过php mysql导出csv中的多行

javascript - 使用 PHP 的 AJAX - 不同的结构方式?

PHP set_error_handler() 总是返回 null

php - 正则表达式太大

购买两个以上元素时的 Paypal 折扣(不是同一对象的两个实例)

Php QR 代码库不工作?

php - 插入不同服务器上的 MySQL 数据库

php - 试图将更新数据放入 CI 中的多个表中

c# - 使用 PayPal REST API ASP.NET MVC 一次支付多个项目

php - 购物车数据库设计: adding functionality for quantities by size