title: PHP/MySQL - Doesn't get the first ID in the table on button click
body: <p>We are creating an HTML table where if a user selects a "Checkout" button next to a name, it will update a row in the database with the current date and time while removing them from the HTML table. It does this by grabbing ID of the row selected. For some reason, when there are more than one names in the HTML table, if you click the Checkout button of the first one, it grabs the ID of the name below it and updates that row in the database. If you click the Checkout button of the one below the first one, then it does executes the code as normal.</p>
  
  <p>Page code:</p>
  
  <pre><code>    &lt;?php
      // connect to database
      require_once('connection.php');
      session_start();
  
      //get session variable, if empty, unset and logout
      if(empty($_SESSION['department'])) {
      session_unset();
      session_destroy();
      header("Location: index.php");
      } else {
      }
  
      $dept = $_SESSION[department];
      $eventsTable = $dept . "_events";
      $checkinTable = $dept . "_checkin";
  
      $query = mysqli_query($VisitorManagement, "SELECT * FROM config WHERE deptCode='$dept'");
      $row2 = mysqli_fetch_array($query);
      $checkout = $row2['checkout'];
  
      $sql = mysqli_query($VisitorManagement, "SELECT * FROM {$checkinTable} WHERE deptCode='$dept'");
      $row3 = mysqli_fetch_array($sql);
      $checkoutDateTime = $row3['checkoutDateTime'];
  
      if (!$checkout) {
      header("Location: checkin.php");
      }
  
      if(isset($_POST['checkoutUser']))
      {
      $id = mysqli_real_escape_string($VisitorManagement, $_POST['id']);
      // set checkout date
      $query = "UPDATE {$checkinTable} SET checkoutDateTime = CURRENT_TIMESTAMP() WHERE id = '$id'";
      if (mysqli_query($VisitorManagement, $query)) {
          header('Location: checkout-thankyou.php');
      } else {
          echo "Error: " . $sql . "&lt;br&gt;" . mysqli_error($VisitorManagement);
      }
      }
  
  
      ?&gt;
  
      &lt;!doctype html&gt;
      &lt;html class="no-js" lang="en"&gt;
      &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;meta http-equiv="x-ua-compatible" content="ie=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;
      &lt;title&gt;Visitor Management&lt;/title&gt;
      &lt;link rel="stylesheet" href="css/foundation.min.css" /&gt;
      &lt;link rel="stylesheet" href="css/app.css" /&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;!-- nav --&gt;
      &lt;div class="top-bar"&gt;
      &lt;div class="top-bar-left"&gt;
      &lt;ul class="menu"&gt;
      &lt;li&gt;&lt;a href="checkin.php"&gt;Check-In&lt;/a&gt;&lt;/li&gt;
      &lt;?php 
          if ($checkout) {
      ?&gt;
      &lt;li&gt;&lt;a href="checkout.php" class="active"&gt;Checkout&lt;/a&gt;&lt;/li&gt;
      &lt;?php } ?&gt;
      &lt;/ul&gt;
      &lt;/div&gt;
      &lt;div class="top-bar-right"&gt;
      &lt;ul class="menu"&gt;
        &lt;li&gt;&lt;a href="login.php"&gt;Admin Login&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="logout.php"&gt;Logout&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row text-center" style="margin-top: 5%;"&gt;
          &lt;h1&gt;&lt;img src="img/su-wordmark-vm.png" alt="Syracuse University"&gt;&lt;/h1&gt;
          &lt;?php
      $sql = "SELECT name from departments WHERE code='{$dept}'";
      $result = mysqli_query($VisitorManagement, $sql);
      $row = mysqli_fetch_array($result);
      $name = $row["name"];
      echo "&lt;h2&gt; $name &lt;/h2&gt;"
      ?&gt;
        &lt;/div&gt;
  
  
         &lt;div class="row"&gt;
      &lt;div class="medium-12 columns text-center" style="padding-top: 5%;"&gt;
        &lt;p&gt;Please select the checkout button of the visitor you wish to checkout.&lt;/p&gt;
  
        &lt;form id='checkout' method='post' name='checkout'&gt;
         &lt;?php
                   $checkinTable = $dept . "_checkin";
  
                  $result = mysqli_query($VisitorManagement, "SELECT * FROM {$checkinTable} WHERE DATE(checkinDateTime) = CURDATE() AND checkoutDateTime IS NULL ORDER BY checkinDateTime DESC");
  
                  echo "&lt;table class='checkin'&gt;
                  &lt;thead&gt;
                  &lt;tr&gt;
                  &lt;th&gt;Student Name&lt;/th&gt;
                  &lt;th&gt;SUID #&lt;/th&gt;
                  &lt;th&gt;Student Email&lt;/th&gt;
                  &lt;th&gt;Primary Affiliation&lt;/th&gt;
                  &lt;th&gt;Program&lt;/th&gt;
                  &lt;th&gt;Education Level&lt;/th&gt;
                  &lt;th&gt;Staff Member&lt;/th&gt;
                  &lt;th&gt;Reason for Visit&lt;/th&gt;
                  &lt;th&gt;Check In Date/Time&lt;/th&gt;
                  &lt;th&gt;Checkout&lt;/th&gt;
                  &lt;th aria-hidden='true' class='hide'&gt;Hidden User ID&lt;/th&gt;
                  &lt;/tr&gt;
                  &lt;/thead&gt;";
  
  
                      while($row = mysqli_fetch_array($result))
                      {
                          $phpdate = strtotime($row['checkinDateTime']);
                          $dateTime = date('F j, Y, g:i a', $phpdate);
                                  echo "&lt;tbody&gt;";
                                  echo "&lt;tr&gt;";
                                  echo "&lt;td&gt;" . $row['studentName'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;" . $row['suid'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;" . $row['studentEmail'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;" . $row['studentAffiliation'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;" . $row['studentProgram'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;" . $row['studentEduLevel'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;" . $row['staffMember'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;" . $row['reasonForVisit'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;" . $dateTime . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;input type='submit' class='button' id='checkoutUser' name='checkoutUser' value='Checkout'&gt;&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;input type='hidden' aria-hidden='true' name='id' value='" . $row['id'] . "'&gt;&lt;/td&gt;";
                                  echo "&lt;/tr&gt;";
                  }
               echo "&lt;/tbody&gt;";   
               echo "&lt;/table&gt;";
          echo "&lt;/form&gt;";
  
                  mysqli_close($VisitorManagement);
              ?&gt;  
      &lt;/div&gt;
    &lt;/div&gt;
      &lt;script src="js/vendor/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="js/vendor/what-input.min.js"&gt;&lt;/script&gt;
      &lt;script src="js/foundation.min.js"&gt;&lt;/script&gt;
      &lt;script src="js/app.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Any ideas on why this may be happening?</p>