1

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' ");

6 Answers 6

2

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'
Sign up to request clarification or add additional context in comments.

3 Comments

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...
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.
Ah, that was not clear to me from his example code. I supplied an answer for both options.
1
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'"

Comments

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

or

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

Comments

0

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

Comments

0
mysql_send("UPDATE peopletable SET amount=`amount`+6 WHERE id='100' ");

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.