I'm very new to PHP and am trying to create a laptop signing in and out system. I want to be able to update the status of the laptop from 0 to 1 and vice versa depending on if that laptop is on loan or not. I've not written an UPDATE query before and cannot seem to get the value to update when the form is submitted. I'm also trying to use PDO and again am not sure if I'm using this correctly. I also seem to just be targeting the last item in the array rather than the corresponding laptop that I'm pressing the submit on.
I've tried to piece together different things from different answers and forums that I've read but have no idea if I'm going in the right direction. I know I'm connected to the database and am able to pull information from it but have not been able to get it to update. I know the form posts because I get the echo $laptop_name but like I said previously this is the last row rather than the button I'm pressing on.
<?php
if(isset($_POST['borrow'])) {
// echo '<script language="javascript">';
// echo 'alert("hi")';
// echo '</script>';
$STH = $DBH->prepare("SELECT * FROM laptops");
$STH->execute(array());
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
while ($row = $STH->fetch()) {
$laptop_name = htmlspecialchars($row->laptop_name);
$laptop_id = htmlspecialchars($row->laptop_id);
$status = htmlspecialchars($row->status);
}
$STH = $DBH->prepare("UPDATE laptops SET status = 0
WHERE laptop_name = ? LIMIT 1");
$STH->execute(array($laptop_name));
echo $laptop_name;
}
$STH = $DBH->prepare("SELECT * FROM laptops");
$STH->execute(array());
// WHILE LOOP TO LOOK THROUGH THE DIFFERENT ROWS
while ($row = $STH->fetch()) {
$laptop_name = htmlspecialchars($row->laptop_name);
$laptop_id = htmlspecialchars($row->laptop_id);
$status = htmlspecialchars($row->status);
?>
<form class="" action="" method="post">
<div class=""><?php echo $laptop_name; ?></div>
<div>
<?php
if($status == 1) {
echo "Available";
?>
<input type="submit" name="borrow" value="Borrow Laptop">
<?php
} else {
echo "Not Available";
}
?>
</div>
</form>
<?php
}
?>
I've gotten rid of the error and variable as this is now marked as a duplicate and I don't know if it's based on that - I've spent a fair while looking through similar questions and have tried to take information from these but like I said I'm very new and would really appreciate some help please.