15.05.2014, 12:59
You should set a composite UNIQUE key, e.g. userid + itemid. That's was that function was designed for. Then you can use the construct:
Let's say that there is a row with userid = 1 and itemid = 1
Inserting the following will work:
userid 1 and itemid 2
userid 2 and itemid 1
But if you try to insert userid 1 and itemid 1 again, it will fail and use the update clause instead, e.g.
Affected rows will then return: 1 if the row was inserted or 2 if an existing row was updated.
See also here: https://sampforum.blast.hk/showthread.php?tid=505081. Section 'Setting limits'.
PHP код:
INSERT ... ON DUPLICATE KEY UPDATE ...
Inserting the following will work:
userid 1 and itemid 2
userid 2 and itemid 1
But if you try to insert userid 1 and itemid 1 again, it will fail and use the update clause instead, e.g.
PHP код:
INSERT INTO inventory (userid, itemid, amount) VALUES (1,1,42) ON DUPLICATE KEY UPDATE amount = amount + 42
See also here: https://sampforum.blast.hk/showthread.php?tid=505081. Section 'Setting limits'.