【问题标题】:Shopping cart deletes all quantities instead of one购物车删除所有数量而不是一个
【发布时间】:2015-11-22 22:01:59
【问题描述】:

我正在尝试用 PHP 创建一个简单的购物车。我设法将项目添加到会话中。如果您添加相同的项目,数量会更新。

但是,当我删除数量超过 1 的项目时,它将删除该项目的所有内容,而不是从数量中删除 1。

我想知道是否有人可以检查我可能做错了什么?

获取产品的ID:

<a href="checkout.php?id=<?php echo $earthProducts -> id; ?>"> Order Now </a>

我有一个 Item 类:

<?php
Class Item{
var $id;
var $name;
var $price;
var $quantity;
}
?>

在结帐页面上,它将显示当前在购物车中的所有产品:

require 'item.php';

    if (isset($_GET['id'])) {
        $result = mysqli_query($con, 'SELECT * FROM earthProducts WHERE id=' . $_GET['id']);
        $earthProducts = mysqli_fetch_object($result);
        $item = new Item();
        $item->id = $earthProducts->id;
        $item->name = $earthProducts->name;
        $item->price = $earthProducts->price;
        $item->quantity = 1;

        // Check product is existing in cart
        $index = -1;
        $cart = unserialize(serialize($_SESSION['cart']));
        for ($i = 0; $i < count($cart); $i++)
            if ($cart[$i]->id == $_GET['id']) {
                $index = $i;
                break;
            }
        if ($index == -1) {
            $_SESSION['cart'] [] = $item;
        } else {
            $cart[$index]->quantity++;
            $_SESSION['cart'] = $cart;
        }
    }
    ?>

然后我用一个按钮打印购物车以删除该项目:

<table cellpadding="2" cellspacing="2" border="1">
                    <tr>
                        <th>Option</th>
                        <th>Id</th>
                        <th>Name</th>
                        <th>Price</th>
                        <th>Quantity</th>
                        <th>Sub Total</th>
                    </tr>
                    <?php
                    $cart = unserialize(serialize($_SESSION['cart']));
                    $s = 0;
                    $index = 0;
                    for ($i = 0; $i < count($cart); $i++) {
                        $s += $cart[$i]->price * $cart[$i]->quantity;
                        ?>
                        <tr>
                            <td> <a href="checkout.php?index=<?php echo $index; ?>" onclick="return confirm('Are you sure?')">Delete</td>
                            <td><?php echo $cart[$i]->id; ?></td>
                            <td><?php echo $cart[$i]->name; ?></td>
                            <td><?php echo $cart[$i]->price; ?></td>
                            <td><?php echo $cart[$i]->quantity; ?></td>
                            <td><?php echo $cart[$i]->price * $cart[$i]->quantity; ?></td>
                        </tr>
                        <?php
                        $index++;
                    }
                    ?>
                    <tr>
                        <td colspan="4" align="right">Sum</tr>
                    <td align="left"> <?php echo $s ?></td>
                </table>
                <br>
                <a href="earth_products.php"> Continue Shopping </a>

                <br>
                <br>
                <?php
                print_r($cart);
                ?>

我删除购物车中商品的代码(这是错误的):

// Delete product in cart
    if (isset($_GET['index'])) {
        $cart = unserialize(serialize($_SESSION['cart']));
        unset($cart[$_GET['index']]);
        $cart = array_values($cart);
        $_SESSION['cart'] = $cart;
    }

因此,如果我有数量为 1 的 item-1,我按删除它将删除它。如果我有数量为 2 的 item-2,它将删除这两个数量并从购物车中删除 item-2。

如果有人可以提供帮助,请提前感谢您。

【问题讨论】:

    标签: php shopping-cart


    【解决方案1】:

    您需要在取消设置之前检查数量,这样应该可以:

    if (isset($_GET['index'])) {
        $cart = unserialize(serialize($_SESSION['cart']));
        if ($cart[$_GET['index']]->quantity == 1){
           unset($cart[$_GET['index']]);
        }else{
           $cart[$_GET['index']]->quantity--;
        }
        $cart = array_values($cart);
        $_SESSION['cart'] = $cart;
    }
    

    【讨论】:

      【解决方案2】:

      您需要减少商品的数量,而不是仅仅删除它,因为当数量大于 1 时,您的代码只会将商品从购物车中完全删除。

      另外,在 SQL 代码中直接使用 $_GET 变量是非常危险的,这使得 SQL 注入和转储数据库变得非常容易。

      【讨论】:

      • 感谢您的评论,我现在只是做一个基本的购物车,但肯定会更新我的代码。
      • @Brent 那就好,只是想避免你没想到这一点给你带来麻烦。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多