Reputation: 11
I'm doing a shopping cart system and I'm not sure how to reduce item quantity in mysql when checking out a shopping cart in php?
For example, when 2 of item1 are purchased, the quantity column in mysql should be reduced by 2. i.e. the quantity should be reduced with respect to the quantity purchased.
Upvotes: 0
Views: 6598
Reputation: 4136
See If you multiple products in cart, you should loop through product like below
foreach ($cartItem as $cart) {
$productId = $cart['product_id'];
$qty = $cart['qty'];
$sql = "UPDATE `products` SET `num_of_stocks` = `num_of_stocks` - $qty
WHERE `id` = $productId";
}
Upvotes: 0
Reputation: 53198
You can run a simple MySQL UPDATE
query:
UPDATE `products` SET `quantity` = `quantity` - num_purchased WHERE `id` = 15
Obviously you'll need to replace the values, field names and table names with those you actually use...
Upvotes: 2