Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update quantity in database

Tags:

php

mysql

My MySQL table looks as follows:

Item_Id      Item_Name     quantity
1                  HPWT 20         20

The quantity field is an integer.

Now, if I bought this "HPWT 20" item again with quantity "5" , I want my table affected as follows:

Item_Id      Item_Name     quantity
1                  HPWT 20         25

What I always do is: first I select the previous quantity from the database, then add new quantity to it, then update the quantity with new one at the desired Item_Id.

Is there another (direct) way to Update the quantity?

like image 692
Vikram Avatar asked Jun 10 '26 11:06

Vikram


1 Answers

You can execute a UPDATE directly:

UPDATE table SET quantity = quantity + 5 WHERE Item_id = <x>
like image 195
Hernã Saldanha Avatar answered Jun 12 '26 01:06

Hernã Saldanha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!