-1

In our cart process we don't want to remove items from the store inventory until after the payment gateway has returned a approved code for the credit card purchase. Thus assuming the payment was approved we need the cart items to deduct from the inventory.

In a mysqli UPDATE query how if possible can i write it so that the new value being set is the result of the rows current value - the quantity stored in the carts Item_qty field?

mysqli_query($con,"UPDATE table1 SET Qty='`**current-value**` - $proc[Item_Qty]' WHERE buyer='$user'");

I do understand that I can accomplish this using multiple queries, perform some math and then write the value back to the database, but I am hoping there is a cleaner way with less code by doing all in a single mysqli query.

4

3 回答 3

2

You can use current values of the table in an update.

mysqli_query($con,"UPDATE table1 SET Qty = Qty - $proc[Item_Qty] WHERE buyer='$user'");
于 2013-07-26T15:47:10.490 回答
2

Easy. You can use the field itself in the calculation:

UPDATE table1 SEt Qty=Qty - $proc[Item_Qty]
                      ^^^--- this is 100% perfectly valid
于 2013-07-26T15:47:55.490 回答
1

This should do it:

$query = "UPDATE table1 SET Qty = Qty - $proc[Item_Qty] WHERE buyer='$user'";

You are simply, using the field name again to get its current value.

于 2013-07-26T15:47:31.903 回答