UPDATE `comment`
SET `agree`=`agree`+1,(INSERT INTO `reacted_on` VALUES (10,197))
WHERE `id`=197 and 0 = (SELECT COUNT(*) FROM `reacted_on` WHERE `id_user`=10 and `id_comment`=197)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'(INSERT INTO `reacted_on` VALUES (10,197)) WHERE `id`=197 and 0 = (SELECT COUNT(' at line 1
I want to icrement agree in comment but i want to prevent same user to agree on a comment more than one. Therefore, first I check if user reacted on the same comment; I increment agree. Then I insert a row to prevent user from reacting on the same comment anymore.
What am I missing? why is it not true?
What should I do to fix the problem?
How can I use INSERT statement in UPDATE query?
This is where I use the query:
$con = mysql_connect($my_sql_servername,$my_sql_username,$my_sql_password);
mysql_select_db($my_sql_database, $con);
if($validation != $_SESSION['validation'])
die('-1');
$sql = "IF NOT EXISTS (SELECT * FROM `reacted_on` WHERE `id_user`=".$id_user." and `id_comment`=".$id_comment." ) THEN INSERT INTO `reacted_on` VALUES (10,197); UPDATE `comment` SET `agree`=`agree`+1 WHERE `id`=".$id_comment." END IF;";
//echo $sql;
if(!mysql_query($sql)){die(mysql_error());}
Basically, something like this, probably:
You’ll have to do that using separate statements, but you can wrap them into a single
IFinstruction.EDIT
Another way to check for the existence of rows is to use
SELECT COUNT(*)...(just like you did in your attempted script). So the beginning of the above query would change to: