Automatically update quantity in database after purchase is made.
My code below works when there is one product in the cart. When there is more than one, both quantities of the products are changed to the same value. For example; if i have 2 products. One has a quantity of 5 and the other of 8. When i run the code, both are changed to 4. Please help. I want the code to update each quantity correctly. Here is my code: $producta = mysqli_fetch_assoc($result); //if product id in cart equal product id in database //This is an array going through all the values within array and printing each value foreach($_SESSION['shopping_cart'] as $key => $product): $cartID= $product['id']; $databaseID= $producta['id']; $quantityFromDatabase= $producta['Quantity']; $quantityFromCart =$product['quantity']; $newQuantity=$quantityFromDatabase-$quantityFromCart; $queryUpdate= "UPDATE products SET Quantity = '$newQuantity' WHERE id='$cartID'"; $result = mysqli_query($connect, $queryUpdate); endforeach;