title: Increment a counter in MySQL every time a product item is viewed
body: <p>I have a product page in my website that works from a MySQL database, product items are created on the page as a thumbnail picture is clicked on.</p>
  
  <p>Is there any way to increment a MySQL counter every time the thumbnail picture is clicked on?</p>
  
  <p>If there is a way of doing this can someone please point me in the right direction with regards to coding this?</p>
  
  <p>NOTE: this code is working but this update all products hits count. i need update only viewed product items row in table</p>
  
  <p>my mysql table looks like</p>
  
  <p>table name: images</p>
  
  <p>id | product_name | image | price | hits</p>
  
  <p>here is my php code:</p>
  
  <hr>
  
  <pre><code>&lt;?php
  $connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
  $db = mysql_select_db("user"); // Selecting Database
  
  $result=mysql_query("SELECT id,image,SUBSTRING_INDEX(product_name,' ',4) as product_name FROM images");
  
  mysql_query("update images set hits = hits+1 where product_name = product_name");
  
  $count = 0;
  
  while($res=mysql_fetch_array($result))
          {
  if($count==3) //three images per row
              {
  print "&lt;/tr&gt;";
  $count = 0;
  if($count==0)
  print "&lt;tr&gt;";
  print "&lt;td&gt;";
  
  ?&gt;
  &lt;?php echo"&lt;div&gt;";?&gt;
  
  &lt;?php echo"&lt;img style='heigth:170px; width:200px'src='image1.php?id=$res[id]'&gt;"?&gt;
  &lt;?php echo "&lt;a href=\"getImage.php?id={$res['id']}\"&gt;{$res['product_name']}&lt;h3&gt;Read More&lt;/h3&gt;&lt;/a&gt;";?&gt;
  
  &lt;?php echo"&lt;/div&gt;";?&gt;
  
  &lt;?php
  $count++;
  print "
  &lt;/td&gt;";
  
  }
  
  if($count&gt;0)
  print "&lt;/tr&gt;";
  
  ?&gt;
  </code></pre>
  
  <p>here is getImage.php code`</p>
  
  <hr>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;HTML&gt;
  &lt;HEAD&gt;
  
  &lt;style&gt;
  img.floatLeft 
  { 
  float: left; 
  margin-right: 10px;
  margin-bottom: 1px; 
  padding-left: 2%;
  }
  p.padding {
  padding-left: 2%;
  padding-right: 2%;
  }
  
  h3 { color:#3300FF; font-family: Slab,Georgia,serif;font-size: 20px;
    letter-spacing: .03em;text-transform: none}
  
  div{background-color: white; border-style: ridge; height: 500px; width: 400px; margin-left:auto;
  margin-right:auto;  color: #000000; font: 12px arial, sans-serif; line-height: 18px;text-align: justify;}
  
  &lt;/style&gt;
  &lt;/HEAD&gt;
  &lt;body&gt;
  
  &lt;?php
  if (isset($_GET['id'])) {
  $id = $_GET['id'];
  
  $connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
  $db = mysql_select_db("user", $connection); // Selecting Database
  
  
  $query1 = mysql_query("select * from images where id=$id", $connection);
  
  
  
  
  echo"&lt;div &gt;";
  
  while ($row1 = mysql_fetch_array($query1)) {
  echo"&lt;h3 align='center' color='red'&gt;";
  echo $row1['product_name'];
  echo"&lt;/h3&gt;";
  
  
  echo "&lt;img class='floatLeft'src=image1.php?id=".$row1['id']." width=150 height=140  /&gt;";
  
  
  echo'&lt;p class="padding"&gt;';
  echo $row1['price'];
  echo"&lt;/p&gt;";
  echo"&lt;/div&gt;";
  echo"&lt;/div&gt;";
  
  
  
  ?&gt;
  
  &lt;?php
  }
  }
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>