Im trying to create a small library program. In this program, when i click on the button it should search for all the users that borrow books and the return date will expire today. Then it should update the database and mark them as overdue. After which it will update the blocklist to indicate they are blocked from borrowing anymore books.
The program works in a way, the problem is it will only update the very last entry in the database that meets the criteria.
Here is the code:
<form action="" method="post">
<input type="text" name="authorize" placeholder="Enter confimation" style="width:80%"/>
<input type="submit" name="update" value="update listing"/>
</form>
<?php
if (isset($_POST['update'])) {
$authorize = $_POST['authorize'];
$authorization_checker = "confirm";
if (empty($authorize)) {
echo "<script>alert('Please enter confirmation!'); window.location='library_update.php'</script>";
} // end if empty
else if ($authorize != $authorization_checker) {
echo "<script>alert('Invalid confirmation entered'); window.location='library_update.php'</script>";
} // end if not the same
else {
//get expired dates for all member
$currentdate = date('Y-m-d', strtotime('today'));
$get_dates = "SELECT * FROM users WHERE date ='$currentdate'";
$run_get_dates = mysqli_query($conn, $get_dates) or die(mysql_error());
while ($row = mysqli_fetch_array($run_get_dates)) {
$status = $row['status'];
$id = $row['id'];
} //end while
$update_users = "UPDATE users SET status='overdue' WHERE id=$id";
$run_update_users = mysqli_query($conn, $update_users) or die(mysql_error());
if ($run_update_users) {
$update_list = "UPDATE blocklist SET status='blocked' WHERE id=$id";
$result = mysqli_query($conn, $update_list) or die(mysql_error());
echo "<script>alert('library updated successfully'); window.location='library_update.php'</script>";
}
}
} //ends program
?>
I believe its a problem with the loop.
I researched that I could use a for each loop in order to update each entry one at a time but I have no idea how to implement this.
Can i have some help on how to achieve this.
Prince Davian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.