Updating MySQL column by adding, rather 'replacing'


as the summary already says, - I want to update a certain column on my MySQL database with a certain number that needs to be added to the already existing value in the column, rather replaced in the column. Anyone an idea?

Current code:

  mysql_query("UPDATE dice SET points='$calcdb' WHERE user='$_SESSION[usersession]'");

Variable $calcdb refers to a number that was scored by a tossed cube divided by hundred (if that’s important), therefore the points can’t be replaced. :eek:

UPDATE `dice` SET `points` = `points` + $calcdb WHERE ...

But keep an eye on SQL injections!
An alternative if you aren’t sure if the value already exists (but “user” is a unique/primary key):

INSERT INTO `dice` (`user`, `points`) VALUES ($session, $calcdb) ON DUPLICATE KEY UPDATE `points` = `points` + $calcdb

Awesome thank you/danke.

You’re welcome.

This may be usefull, Rudy has that answered near the end of the thread.