update table with subtraction or addition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

update table with subtraction or addition

Post by Burrito »

I have a table field that shows how many "credits" a user has. I need to update this table according to whether or not they use credits, or buy credits.

I'd like to be able to do it all on the fly with one query, but not sure if that's possible. I know I could do it with two (first select what they have, then use php to add or subtract, then update).

Is it possible to just do it all with one query? I was thinking of a subquery (I have 4.1), but even then not sure how I could throw some php in there to reflect the new amount??

any ideas?

thanks,

Burrito
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

d11 got me there with this:

Code: Select all

$query = &quote;UPDATE table SET credits = (credits+$value) WHERE whatever&quote;;
thx mang!
Post Reply