tags:

views:

47

answers:

6

I have a field in my mysql table that I want to update.

Example: In my database the field amount has a value of 4 and I want to add 6 to the value.

How would I add 6 to the existing value???

This is what I have so far but this just deletes the existing value and adds a new one:

mysql_send("UPDATE peopletable SET amount='$amount' WHERE id='100' ");
+1  A: 

You can do this to add something to a supplied value:

UPDATE peopletable SET amount='$amount' + 6 WHERE id='100'

To update the existing value, do:

UPDATE peopletable SET amount = ammoutn  + 6 WHERE id='100'
RedFilter
his answer should work! You can also SELECT the value, make your operation in php and then update the db with the new calculated value...
Piero
This doesn't do what he wants. He wants to add 6 to the amount currently stored in the column, not change it to be 6 greater than an arbitrary quantity.
Hammerite
Ah, that was not clear to me from his example code. I supplied an answer for both options.
RedFilter
A: 

UPDATE peopletable SET amount = amount + 6 WHERE id = 100

Hammerite
A: 
UPDATE peopletable SET amount=amount+6 WHERE id='100'

or if $amount is the value that you're adding (6 isn't static)

   " UPDATE peopletable SET amount=amount+$amount WHERE id='100'"
Dan Heberden
A: 
mysql_send("UPDATE peopletable SET amount=`amount`+6 WHERE id='100' ");
Salil
A: 
UPDATE peopletable SET amount=amount + 6 WHERE id='100'

or

UPDATE peopletable SET amount+= 6 WHERE id='100'
Thomas Winsnes
A: 

Please make sure that you are sanitising your input and $amount is numeric.

Here's what your query should look like:

mysql_send("UPDATE peopletable SET amount+=$amount WHERE id='100' ");

This will take the original value and add $amount to it.

Cetra