Hello guys

I've coded cart class with cart items and showed cart items in a table

but I had a problem when I try to update the cart.. It updates all the cart items when I'm changing quantity in one of the items


<div class="cartContainer">
        <h1 class="shoppingBag">your shopping bag</h1>

        <h2 class="products-in-cart-info">YOU HAVE (<?= $countProducts ?>) ITEM(S) IN YOUR BAG</h2>
        <?php if (ShoppingCart::checkProductsInCart() >= 1) { ?>
            <div style="width: 990px; margin: 0px auto; float: left;">
                <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
                    <table id="cartTable">
                        <?php foreach ($cartItems as $cartItem) { ?>
                                <td><img width="35px" height="70px" src="img/<?= $cartItem->image ?>"></td>
                                    <?= $cartItem->manufacture . ' ' . $cartItem->series . ' ' . $cartItem->model ?>
                                    <span style="height:10px;display:block;"></span>
                                    <?= $cartItem->price . ' $' ?>
                                <td><?= $cartItem->colorName ?></td>
                                <input type="hidden" name="colorId" value="<?= $cartItem->colorId ?>">
                                    <input type="button" id="minusInCart" class="minus" value="-">
                                    <input id="quantityInCart" class="count" type="text" name="quantity"
                                           value="<?= $cartItem->quantity ?>">
                                    <input type="button" id="plusInCart" class="plus" value="+">
                                <td><?= $cartItem->subtotal . ' $' ?></td>
                                <input type="hidden" name="productId" value="<?= $cartItem->productId ?>">
                                <td><input name="deleteItem" title="Remove this item" type="submit" class="deleteItem"
                        <?php } ?>
                    <input type="submit" id="updateCart" name="updateCart" value="Update Shopping Bag">
                    <input type="submit" id="checkout" name="checkout" value="Proceed To Checkout">
                    <input type="submit" id="emptyCart" name="emptyCart" value="Empty Bag">
        } else {
            echo "<div style='width:20%;margin:0 auto;padding:0;'>
                    <a href='index.php' id='backHome'>Back to home</a>
        } ?>


} elseif (isset($_POST['updateCart'])) {
    ShoppingCart::updateCart($_POST['quantity'], $cartId, $_POST['productId']);

// checkout process

PHP ShoppingCart Class

class ShoppingCart


    public static function updateCart($quantity, $cartId, $productId)
        $dbh = mySqlDatabase::getConnectionObject();
        $sql = "UPDATE cartitems
                SET quantity =:quantity
                WHERE productId = :productId
                AND cartId = :cartId";
        $stmt = $dbh->prepare($sql);
        $data = array(
            ':quantity' => $quantity,
            ':productId' => $productId,
            ':cartId' => $cartId

and thanks in advance.

A problem I see here is related to the attributes id and name which are the same for all the items, an id must be unique over the page, regarding the name you can have multiples in case of radio buttons because of the nature of this type of input field: if it is not checked is not sent to the $_POST array:

$items  = array('oranges', 'apples', 'coconut');
foreach($items as $item)
    echo "<input type='radio' name='fruit' value='".$item."' />";

When sending it outputs only the checked:

    [fruit] => apples
    [submit] => submit

When using a text input field, instead, since $_POST is an array, the index name will overwrite all the previous and return always the last:

foreach($items as $item)
    echo "<input type='text' name='fruit' value='".$item."' />";


    [fruit] => coconut
    [submit] => submit

Now, I suppose, you're using javascript to increment and decrement values, correct? By referring to the id, and being repeated over the page it's probably creating some unexpected behaviour. You could check the output of the form by applying this:

echo "<pre>" . print_r($_POST, true) . "</pre>";

So, you can see what is sent.

thx very much I forgot how to test the code !
but I think I've found the answer about how to update with loop like foreach($items as $item)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.