Odie Odie - 11 days ago 6
MySQL Question

If a column has a value add via update

I have a column

transfer_quantity
which has an initial value of 0. When I input some number on my input field

<input type="number" name="qtyBuy">


I want to update my
transfer_quantity
to my
qtyBuy
value.

Here is my initial code:

UPDATE stock_transfer SET stocks_transferred = $qtyBuy WHERE transfer_product_id = 1 ON DUPLICATE KEY UPDATE stocks_transferred = stocks_transferred + $qtyBuy;


Here's an example:

|stocks_transferred|
| 0 |


When I input 10, this should be the result:

|stocks_transferred|
| 10 |


and if I input 25, it should add with the 10 in the

|stocks_transferred|
| 35 |


How do I do this?

Answer
UPDATE stock_transfer SET stocks_transferred = stocks_transferred + $qtyBuy WHERE transfer_product_id = 1