Question

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?

Was it helpful?

Solution

reads is a reserved word in MySQL. Escape it with backticks.

UPDATE cmsss_news_articles 
SET `reads` = '$reads' 
...

OTHER TIPS

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>";
    } 

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>";
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top