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

时间:2017-11-07 11:48:20

标签: php mysql shopping-cart

我试图找出如何在不更新其他产品的情况下更新我的购物车。我是否应该在这里开会?我当前的问题是,每当我更改数量时,它也会更新其他产品,并将框中的数量设置为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 } ?>

1 个答案:

答案 0 :(得分:1)

您的表单需要包含您希望增加数量的产品的指标。例如,像这样:

 <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。我认为你必须在会话中存储每件产品的金额,而不仅仅是你现在正在做的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;