Reputation:
Hi i have a reads counter, but i always get an MySQL error:
MySQL Error: 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 ''reads') VALUES ('2') WHERE id = '20'' at line 1
$reads = $row['reads']+1;
$newsid = $row['id'];
if(!$query = $db->query("UPDATE cmsss_news_articles SET reads = '$reads' WHERE id = '$newsid'")) {
echo "<center><b>Error, cant update row</b></center>";
}
Can you please help me where is the mistake?
Upvotes: 0
Views: 63
Reputation: 2910
You can also loose the increment variable to gain some performance and simplicity.
$newsid = $row['id'];
if(!$query = $db->query("UPDATE cmsss_news_articles SET `reads` = `reads` + 1 WHERE id = '$newsid'")) {
echo "<center><b>Error, cant update row</b></center>";
}
Upvotes: 0
Reputation: 5034
Reads is a reverse key word in MySQL, hence put that in backquotes.
try this:
if(!$query = $db->query("UPDATE cmsss_news_articles SET `reads` = '$reads' WHERE id = '$newsid'")) { ^^
echo "<center><b>Error, cant update row</b></center>";
}
Upvotes: 0
Reputation: 204784
reads
is a reserved word in MySQL. Escape it with backticks.
UPDATE cmsss_news_articles
SET `reads` = '$reads'
...
Upvotes: 5