简体   繁体   中英

MySQL - I have two query but one doesn't work, and one does. Help me please?

if ($_GET['action'] == "like")
{
mysql_query("UPDATE blog SET like=like+1 WHERE id=".$_GET['id']."");
header('Location: blog.php?id='.$_GET['id'].'');
}
else if ($_GET['action'] == "dislike")
{
mysql_query("UPDATE blog SET dislike = dislike+1 WHERE id = ".$_GET['id']."");
header('Location: blog.php?id='.$_GET['id'].'');
}

The "dislike" action works great! But the "like" one doesn't. It's close to be the same thing?

Can someone help me???

LIKE is a keyword. Use backticks :

UPDATE blog SET `like`=`like`+1 ...

In general, it's much better not to name column after keywords ( LIKE,CASE,SELECT,WHERE , etc).

Example

mysql_query("UPDATE blog SET `like`=`like`+1 WHERE id='".
       mysql_real_escape_string($_GET['id'])."'");

Or if your id is integer, you can do in this particular case just .... WHERE id=".(int)$_GET['id']

A good plan would be to check the return value from mysql_error() to get the actual error from MySQL (You can check this by simply echo'ing mysql_error()).

Other than that you really want to throw in an exit() after the call to header() to actually terminate the execution of the script right there, and you also want to add mysql_real_escape_string to escape the GET-arguments you're passing in to MySQL. You do not want to use user supplied data like that unescaped or unfiltered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM