davidjones1990 Posted September 24, 2011 Share Posted September 24, 2011 Hey everyone, So here is my problem. I have some code to display the amount of views that page has got. In this case it is the thread in my forums section. I have used the same code to show how many people have views a certain persons profile page and that works fine but when I use it on my forum thread page I get this 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 'WHERE id='20' LIMIT 1' at line 1 Here is the section of code: <?php $thread_id = preg_replace('#[^0-9]#i', '', $_GET['id']); $getThreadViews = mysql_query("SELECT view_count FROM forum_posts WHERE id='$thread_id' LIMIT 1") or die (mysql_error()); $row = mysql_fetch_assoc($getThreadViews); $counter = $row['view_count']; if($counter == 0){ $counter = 1; $startCounter = mysql_query("INSERT INTO forum_posts (view_count) VALUES ('$counter') WHERE id='$thread_id' LIMIT 1") or die (mysql_error()); } $threadViews = $counter+1; $appendCounter = mysql_query("UPDATE forum_posts SET view_count='$view_count' WHERE id='$thread_id'") or die (mysql_error()); ?> I have checked that there are no spelling errors so just wanted to show it to a fresh pair of eyes because its really starting to annoy me. Thanks in advance for any help. Quote Link to comment https://forums.phpfreaks.com/topic/247772-getting-errors-on-code-i-have-used-before-page-view-count/ Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 Of the 3 queries, I'm going to assume it's the INSERT query throwing the error, since INSERT queries don't use a WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/247772-getting-errors-on-code-i-have-used-before-page-view-count/#findComment-1272332 Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 If you have the id field uniquely indexed, you might want to look into using INSERT . . . ON DUPLICATE KEY UPDATE syntax instead. Quote Link to comment https://forums.phpfreaks.com/topic/247772-getting-errors-on-code-i-have-used-before-page-view-count/#findComment-1272335 Share on other sites More sharing options...
davidjones1990 Posted September 24, 2011 Author Share Posted September 24, 2011 Hey thanks for the help got it sorted now Quote Link to comment https://forums.phpfreaks.com/topic/247772-getting-errors-on-code-i-have-used-before-page-view-count/#findComment-1272367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.