Sunday, 29 September 2013

Getting a form post checkbox variable outside a while loop on another page

Getting a form post checkbox variable outside a while loop on another page

I have read through this post, which looks to be the most similar, but it
has not solved my problem.
I have the below while loop working to display results from my database.
The specific line of question in the while loop is this (note: it is
within a form):
while($row = mysqli_fetch_array($result)){
<input type='checkbox' name='booking_to_delete[]'
value='".$row['BookingNo']."'>
}
The rest of the form data passes through fine except for the checkboxes.
In the following php page, I have this:
function delete_booking_from_database($num){
$sql="SELECT * FROM bookings WHERE BookingNo=".$num;
$result=mysqli_query($sql);
if (!result){
echo "Record not found.";
}
else{
$sql="DELETE FROM bookings WHERE BookingNo=".num;
$result=mysqli_query($sql);
}
}
if (isset($_POST['booking_to_delete']) &&
count($_POST['booking_to_delete'])>0){
foreach($_POST['booking_to_delete'] as $bookingid){
delete_booking_from_database($bookingid);
}
echo "Record successfully deleted";
header("refresh:2;url=index.php");
}
The page shows "Record successfully deleted" and forwards me back to
index.php so I know that's working fine, however no data is deleted from
the database.

No comments:

Post a Comment