title: Update counter using php and mysql in a session
body: <p><em><strong>Problem:</em></strong> I want to connect two tables files and counter according to the paper_id column from files table connecting with the visitors column from counter table. </p>
  
  <p>There are two pages; publicationView.php and profile.php. 
  When a reader clicks publicationView.php, php code should count the view of the page according to the publication id and only update using session. 
  When a user visits his profile.php, then he can see his publications in a table with the publications' views relatively. </p>
  
  <p>There are two tables; files where all the publications are and counter where the counter are. I am trying to update using the URI of the publication and the URI is saved in counter table. </p>
  
  <p><em><strong>Implementation:</em></strong> </p>
  
  <p>The publicationView.php includes </p>
  
  <pre><code>$webpage=htmlspecialchars($_SERVER["REQUEST_URI"]);
  
   $sql = "CREATE TABLE IF NOT EXISTS counter (
   id int(4) NOT NULL auto_increment,
   webpage varchar(90) NOT NULL,
   visitors int(11) NOT NULL default '1',
   PRIMARY KEY (id)
   ) ENGINE=MyISAM AUTO_INCREMENT=1"; 
  
  mysql_query($sql);
  
  $result=mysql_query("SELECT * FROM counter WHERE webpage='$webpage'");
  $num_rows = mysql_num_rows($result);
  if ($num_rows == 0){
  mysql_query("INSERT INTO counter (id, webpage, visitors)
   VALUES ('','$webpage','1')");
  
   }else{
  
   if (!isset($_SESSION['webpage'])){$_SESSION['webpage'] = 0;
    mysql_query("UPDATE counter SET visitors=visitors+'1' WHERE webpage='$webpage'");}}
  </code></pre>
  
  <p>The profile.php includes </p>
  
  <p>The code to echo our the visitors column from counter table. </p>