购物车删除所有数量而不是一个

Shopping cart deletes all quantities instead of one

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

但是,当我删除数量超过 1 件的商品时,它会删除该商品的所有内容,而不是从数量中删除 1 件。

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

取产品ID:

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

我有一个项目class:

<?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 的商品 1,我按删除键将其删除。如果我有数量为 2 的商品 2,它将同时删除这两个数量并将商品 2 从购物车中移除。

如果有人可以提供帮助,在此先感谢您。

您需要在取消设置之前检查数量,这样的事情应该有效:

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

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

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