I'm new to PHP and mysql so this is probably an easy question to answer.
I'm making a "image A vs image B" site (just so I can learn a few PHP+mysql concepts).
Two images load on screen, you click the one you like most. If you click on image A it gets +1 added to its rating, and image B gets + 1 added to its negative rating. I want the positive votes and negative votes to be separate which is why I don't use just one value.
The table looks like;
imageID | votesUP | votesDOWN
X54vb | 1 | 0
pe9D3 | 0 | 1
If image X54vb
went against image pe9D3
and won, the table would look like the example above.
I want to make one query to increment the value (and create the entry if it doesn't exist).
$voteup
is the image id in this example;
REPLACE INTO images SET votesUP = votesUP + 1, imageID = '" . $voteup . "';
This creates the entry if it doesn't exist, but doesn't increment the value if it does. The value is always exactly 1.
I also tried;
UPDATE images SET votesUP = votesUP + 1 WHERE imageID = '" . $voteup . "';
This increments the value, but doesn't create an entry if it doesn't exist. Nothing seems to happen when the entry matching $voteup
hasn't been created.
So, basically, I'd like to have just one query that will create an entry (if it doesn't exist), then increment it if it does.
I'd rather not attempt to read from the entry to confirm if it exists or not, then create an entry if it doesn't, then increment it. I'd like an efficient single query if possible.
This is the table I've created;
CREATE TABLE images
(
imageID CHAR(15),
votesUP INT DEFAULT 0,
votesDOWN INT DEFAULT 0,
PRIMARY KEY (imageID)
)"
Also, what exactly is the difference between REPLACE INTO
and UPDATE
?
Thanks.