I made something and I wish to know if is another way to do this
$this = $CURUSER["id"];
mysql_query("UPDATE wuploaders SET uploads=uploads+1 WHERE userid=$this") or die(mysql_error());
if (mysql_affected_rows() == 0)
mysql_query("INSERT INTO wuploaders (userid, uploads) VALUES ('$this','1')") or die(mysql_error());
All the best, Zamfir
INSERT INTO wuploaders (userid, uploads) VALUES ('$this','1')
ON DUPLICATE KEY UPDATE uploads = uploads+1
It will work if userid is a unique/primary key
If you already have the upload count, you could also use REPLACE.
REPLACE INTO wuploaders (userid, uploads) VALUES ($userid, $count);
The solution of @a1ex07 might be good either way though :)
In addition to the INSERT..ON DUPLICATE KEY UPDATE
syntax that others have mentioned, sometimes it's appropriate to use REPLACE
. It's basically a DELETE of the row followed by an INSERT of the new values.
This has some side effects, like firing both a delete and insert trigger. It also is subject to referential integrity constraints, so if there are any child rows referencing the row you're replacing, either the REPLACE will fail, or else the delete will cascade and also delete the child row.
There are still times when REPLACE is handy, but keep those issues in mind.