Jump to content

Dynamic hit counter


stuieb

Recommended Posts

Hi,

 

I'm new here but really do need some help.

 

I have a site that is PHP and MySQL and it has detail pages that are served via a url string like view_page.php?recordID=xxx.

I want to insert an update comand on the page to count hits. I know the difference between hits and page views etc, but just need to count the number of times the page is loaded (by whatever means).

 

I have the code as follows which can be used in a static page:

 

mysql_query("UPDATE tabel set hits = hits+'1'");

 

but I can't figure out how to do a WHERE id=(url variable recordID) so that just that record in the database gets updated.

 

Please help.

 

Many thanks in advance,

Stu.

Link to comment
https://forums.phpfreaks.com/topic/98813-dynamic-hit-counter/
Share on other sites

I'm not sure if this is the best method to use, but you could use something like:

 

if(isset($_GET['recordID'])){
      $id=@mysql_real_escape_string($_GET['recordID']);
      if((mysql_num_rows(mysql_query("SELECT id FROM table WHERE id='$id'")))==0){
      echo 'Error Message';
      } else {
      @mysql_query("UPDATE table set hits=hits+1 WHERE id='$id'");
      }
}

 

I haven't tested the code, but give it a go :)

 

Link to comment
https://forums.phpfreaks.com/topic/98813-dynamic-hit-counter/#findComment-505635
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.