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.
Page code:
<?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 . "<br>" . mysqli_error($VisitorManagement);
}
}
?>
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Visitor Management</title>
<link rel="stylesheet" href="css/foundation.min.css" />
<link rel="stylesheet" href="css/app.css" />
</head>
<body>
<!-- nav -->
<div class="top-bar">
<div class="top-bar-left">
<ul class="menu">
<li><a href="checkin.php">Check-In</a></li>
<?php
if ($checkout) {
?>
<li><a href="checkout.php" class="active">Checkout</a></li>
<?php } ?>
</ul>
</div>
<div class="top-bar-right">
<ul class="menu">
<li><a href="login.php">Admin Login</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</div>
</div>
<div class="row text-center" style="margin-top: 5%;">
<h1><img src="img/su-wordmark-vm.png" alt="Syracuse University"></h1>
<?php
$sql = "SELECT name from departments WHERE code='{$dept}'";
$result = mysqli_query($VisitorManagement, $sql);
$row = mysqli_fetch_array($result);
$name = $row["name"];
echo "<h2> $name </h2>"
?>
</div>
<div class="row">
<div class="medium-12 columns text-center" style="padding-top: 5%;">
<p>Please select the checkout button of the visitor you wish to checkout.</p>
<form id='checkout' method='post' name='checkout'>
<?php
$checkinTable = $dept . "_checkin";
$result = mysqli_query($VisitorManagement, "SELECT * FROM {$checkinTable} WHERE DATE(checkinDateTime) = CURDATE() AND checkoutDateTime IS NULL ORDER BY checkinDateTime DESC");
echo "<table class='checkin'>
<thead>
<tr>
<th>Student Name</th>
<th>SUID #</th>
<th>Student Email</th>
<th>Primary Affiliation</th>
<th>Program</th>
<th>Education Level</th>
<th>Staff Member</th>
<th>Reason for Visit</th>
<th>Check In Date/Time</th>
<th>Checkout</th>
<th aria-hidden='true' class='hide'>Hidden User ID</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
$phpdate = strtotime($row['checkinDateTime']);
$dateTime = date('F j, Y, g:i a', $phpdate);
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['studentName'] . "</td>";
echo "<td>" . $row['suid'] . "</td>";
echo "<td>" . $row['studentEmail'] . "</td>";
echo "<td>" . $row['studentAffiliation'] . "</td>";
echo "<td>" . $row['studentProgram'] . "</td>";
echo "<td>" . $row['studentEduLevel'] . "</td>";
echo "<td>" . $row['staffMember'] . "</td>";
echo "<td>" . $row['reasonForVisit'] . "</td>";
echo "<td>" . $dateTime . "</td>";
echo "<td><input type='submit' class='button' id='checkoutUser' name='checkoutUser' value='Checkout'></td>";
echo "<td><input type='hidden' aria-hidden='true' name='id' value='" . $row['id'] . "'></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</form>";
mysqli_close($VisitorManagement);
?>
</div>
</div>
<script src="js/vendor/jquery.min.js"></script>
<script src="js/vendor/what-input.min.js"></script>
<script src="js/foundation.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
Any ideas on why this may be happening?