title: Viewcount with mysql and php
body: <p>I have a form with a dropdown menu , that has different genres of movies , this is the code for that :
  </p>
  
  <pre><code> &lt;select name="genero"&gt;&lt;?php 
  
   if ($result-&gt;num_rows &gt; 0) {
  
      while($row = $result-&gt;fetch_assoc()) {
         if (!empty($_POST['genero']) &amp;&amp; $row['nombre'] == $_POST['genero']) {
          $selected = 'selected="selected"';
      } else {
          $selected = '';
      }
      echo '&lt;option '.$selected.' value="'.$row["nombre"].'"&gt;'.$row["nombre"].'&lt;/option&gt;';}
  } else {
      echo "0 results";
  }
  
  
  
  
   ?&gt;
  </code></pre>
  
  <p>Then i have a isset that controls that a specific genre has been selected</p>
  
  <pre><code>    if (isset($_POST['genero'])) {
  
  
          $genero=$_POST['genero'];
          $sql = "UPDATE generos SET visitas=visitas+1 where nombre='$genero'";
          if ($conn-&gt;query($sql) === TRUE) {
                   $ip2 = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
                  } else {
                  echo "Error sumando visita: " . $conn-&gt;error;}
  </code></pre>
  
  <p>now here's the thing , when a user first enters said form i store their ip ( the way it's stored does not need to be super secure so that's why i do it like this)</p>
  
  <pre><code>$ip = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
  </code></pre>
  
  <p>that's why when the UPDATE query is successfully executed i store that ip again.
  That update what does is serve has my count for views , so i can know how many clicks a specific genre has , but the way the code works now makes it useless since a user can click 50million times on one genre at it would count them.
  What i'm trying to do is compare the fist ip to the second ip and if the ip is the same do a query that subtract 1 to that genre so only 1 is added 
  this is what i've tried</p>
  
  <pre><code>     $contador=$contador+1;
      if (isset($_POST['genero'])) {
  
          $genero=$_POST['genero'];
          $sql = "UPDATE generos SET visitas=visitas+1 where nombre='$genero'";
  
          if ($conn-&gt;query($sql) === TRUE) {
                   $ip2 = $_SERVER['REMOTE_ADDR']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['HTTP_CLIENT_IP']);
  
                  } else {echo "Error sumando visita: " . $conn-&gt;error;}      
  
                  if ($contador&gt;1) {
                       if ($ip2==$ip){
                           $sql = "UPDATE generos SET visitas=visitas-1 where nombre='$genero'";
                           if ($conn-&gt;query($sql) === TRUE) {}
  
                           }
  
  
                      }
  </code></pre>
  
  <p>then i tried something like this with isset , checking that the first update had been done already</p>
  
  <pre><code>    if (isset($ip2) and ($ip==$ip2)) {
                      $sql = "UPDATE generos SET visitas=visitas-1 where nombre='$genero'";
                      if ($conn-&gt;query($sql) === TRUE) {}}
  </code></pre>
  
  <p>but what happened is that the number stays the same since it adds 1 and substracts 1</p>