-1

I want to show the page views in a news site. That's why there is a cell name views.

I am trying the codes

$sql = "UPDATE posts SET views=views+1 WHERE id=17184"; 

in the singlepage.php page. But it's not working. Where to use these codes?

Some points for more detail...

  1. I want to increase the value of views by 1 in every reload/refresh of the page singlepost.php

  2. When someone click on the links from index.php then singlepost.php open with the detail of that link

  3. Let's see where I am using the codes and how I am using...

                        $sql = "UPDATE posts SET views=views+1 WHERE id=17184";
    
Uzzwal Dhali
  • 71
  • 1
  • 8
  • possible duplicate of [Increment a database field by 1](http://stackoverflow.com/questions/2762851/increment-a-database-field-by-1) – marijnz0r Apr 29 '15 at 09:55
  • Please provide more information, such as any errors you're receiving. Can't do much with 'it's not working'. – John Bell Apr 29 '15 at 10:04
  • The SQL looks good. Only think I could come up with: leave out the `'` around the field-names. – DocRattie Apr 29 '15 at 10:07
  • Actually I have added the above code in a page called singlepage.php @JohnnyBell The page is retrieving data from the table posts depending on the ID's. The page singlepost.php is displaying several data like headline, post body, date etc. And I haven't received any error!!! :) – Uzzwal Dhali Apr 29 '15 at 10:09
  • 'posts' is a string, as are 'views' and 'id' – Strawberry Apr 29 '15 at 10:14
  • Note: This is a bad solution. First of all, making it a little faster would be updating the views via AJAX. To make the page load faster. However, since you are updating the main posts table, it gets locked. So if you show a list of posts on the front page, its gonna be slower. This applies on a high traffic of course, but still. – Kalle H. Väravas Sep 16 '15 at 21:10

2 Answers2

3

You have ' around your field names which will generate a MySQL error. I suspect you were looking for the backtick which will stop MySQL from treating reserved words as reserved words (ie: if you have a table called update, you would wrap the table name in backticks)

Most likely your issue lies with your query. Below I've updated it for you.

$sql = "UPDATE `posts` SET `views`=`views`+1 WHERE `id`=17184"; 
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

Use this query:

 UPDATE `posts` Set `views`=`views`+1 Where id='17184'
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Pranay Aher
  • 444
  • 4
  • 11