0

I have a PHP script which processes a form with a single input.

I want the value I send through input to be added to an existing value in database (MySQL).

For example in database I have value = 10

When I type into the input 25 and submit the info. I want the value in database to be 35. I don't need any PHP code. I just want to know the correct MySQL query.

3
  • There can be 2 problems. 1) Not existing record You want to add to, so You need to create new record instead. 2) The field in the record You want to add to contains NULL (as default value for example). – Akina May 15 '18 at 10:59
  • Value column is null: UPDATE value = IFNULL(value,0) + $newvalue – jla Nov 11 '20 at 17:13
  • Row doesn't exist: Use some kind of UPSERT like MySQL's ON DUPLICATE KEY UPDATE or just query first and insert or update as appropriate. – jla Nov 11 '20 at 17:17
3
UPDATE value =  value + $newvalue 
0

I had a similar issue this is what I did , I hope it helps other people.

UPDATE table1 SET  value = IF('$_POST[value]'='', value,value + '$value')
0

Cool, it works! I modified it to

UPDATE table1 SET  value = IF('$_POST[value]'='', value - value,value + '$value')

to easily clear "value" if POST is empty

Not the answer you're looking for? Browse other questions tagged or ask your own question.