title: PHP voting, one vote per IP per day: code help needed
body: <p>I am working on a very basic site which will allow people to vote once per 24 hours on an item (game) per IP address. Votes are stored in a table named votes, with columns containing vote ID, game ID, votedate, and IP address.</p>
  
  <p>Users go to the vote page for that game via ...vote.php?id=# (where # is the gid). Here is a simplified version of my current vote.php, which is not working:</p>
  
  <pre><code>&lt;?php
  $gid = $_GET["id"];
  $ip = $_SERVER['REMOTE_ADDR'];
  require_once ('config.php');
  $q = "SELECT * FROM votes WHERE (gid=$gid)";
  $r = @mysqli_query($dbc, $q);
  
  if ($r) {
  while ($row = mysqli_fetch_array($r)) {
  
      if ($row['ip'] != $ip) { // If no IP exists, go ahead and vote.
  
          $q2 = "INSERT INTO votes (gid, votedate, ip) VALUES ('$gid', NOW(), '$ip' )";
          $r2 = @mysqli_query($dbc, $q2);
          if ($r2) {
                  echo 'Thank you for voting';
          } else {
              echo 'There was a problem with your vote'
              }
  
      } elseif ($ip == $row['ip']) {
      // If IP address exists, check date of last time they voted for this game
  
          // Define the two dates
          $votedate = $row['votedate'];
          $now = date("Y-m-d H:i:s");
          // Calculate hours between the dates
          $diff = round((strtotime($now) - strtotime($votedate)) / (60 * 60));
          // Create variable for when user can return.
          $return = '24' - $diff;
  
          if ($diff &gt; '24') { // Allow to vote once every 24 hours
              $q3 = "INSERT INTO votes (gid, votedate, ip) VALUES ('$gid', NOW(), '$ip' )";
              $r3 = @mysqli_query($dbc, $q3);
  
              // Display outcome of insert query:
              if ($r3) {
                  echo 'Thank you for voting, please come back in 24 hours to vote again!';
              } else {
                  echo 'There was a problem with your vote:' . mysqli_error($dbc) . ' in reference to query ' . $q3;
              }
          } else {
              echo 'Sorry, you\'ve already voted in past 24 hours, please come back again in ' . $return . ' hours.';
          }
  
      } 
  
  } // End of while $r loop
  
  } else {
  
  echo 'Sorry, your query did not work.';
  
  } // End of overall if $r ran
  
  mysqli_close($dbc);
  
  exit();
  ?&gt;
  </code></pre>
  
  <p>My goal is for it to work thus:</p>
  
  <p>Grab gid from URL, and find all votes associated with that gid. If the user has never voted for that gid before, then allow new vote to be added. If the user has voted for that gid
  check date of previous vote(s). If last vote is older than 24 hours, allow another vote to be cast for that gid. If last vote was within 24 hours, deny new vote for that game.</p>
  
  <p>Love to hear your thoughts on this...</p>
  
  <hr>
  
  <p>I've looked at other voting systems but I was unable to adapt the jQuery, AJAX, etc. systems to what I wanted, so I figured a really basic PHP system is best for my limited understanding. I also want to be able to list only votes which have been cast in the past 30 days, and other voting plugins' database designs didn't allow for this. Still, I'd be happy if anyone has a fancier alternative--as I know this one is clunky.</p>
  
  <p>Cheers!</p>