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


UPDATE value =  value + $newvalue 

something like this would be right ?

  • 3
    If you want to add the new value to everything in your table, then your query would be correct. If you want to update only specific rows, then you need a WHERE clause. – Taryn Feb 13 '14 at 19:32
  • Tried and it worked. Post it as an answer with sample code so I can choose your answer. Cheers. – Adrian George Feb 13 '14 at 19:35
  • 2
    You can post it as your own answer, you had already solved the problem when you asked the question. Then you can accept it as well. – Taryn Feb 13 '14 at 19:36
  • 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
UPDATE value =  value + $newvalue 
| improve this answer | |

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')
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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