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
HTML
<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">
<tr>
<thead>
<th>product</th>
<th>description</th>
<th>color</th>
<th>quantity</th>
<th>subtotal</th>
<th>remove</th>
</thead>
</tr>
<tbody>
<?php foreach ($cartItems as $cartItem) { ?>
<tr>
<td><img width="35px" height="70px" src="img/<?= $cartItem->image ?>"></td>
<td>
<?= $cartItem->manufacture . ' ' . $cartItem->series . ' ' . $cartItem->model ?>
<span style="height:10px;display:block;"></span>
<?= $cartItem->price . ' $' ?>
</td>
<td><?= $cartItem->colorName ?></td>
<input type="hidden" name="colorId" value="<?= $cartItem->colorId ?>">
<td>
<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>
<td><?= $cartItem->subtotal . ' $' ?></td>
<input type="hidden" name="productId" value="<?= $cartItem->productId ?>">
<td><input name="deleteItem" title="Remove this item" type="submit" class="deleteItem"
value=""></td>
</tr>
<?php } ?>
</tbody>
</table>
<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">
</form>
</div>
<?php
} else {
echo "<div style='width:20%;margin:0 auto;padding:0;'>
<a href='index.php' id='backHome'>Back to home</a>
</div>";
} ?>
</div>
PHP
} elseif (isset($_POST['updateCart'])) {
ShoppingCart::updateCart($_POST['quantity'], $cartId, $_POST['productId']);
// checkout process
}
PHP ShoppingCart Class
class ShoppingCart
{
public
$Id,
$CustomerId,
$CartDate,
$IsComplete,
$IsShipped;
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
);
$stmt->execute($data);
}
}
and thanks in advance.