You can try this:
<?php
/* ESTABLISH CONNECTION */
$connect=mysqli_connect("localhost","root","adminpass","flip");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
if(isset($_POST['delete'])){ /* IF DELETE IS CLICKED */
$checkbox=$_POST['checkbox'];
$hiddencounter=mysqli_real_escape_string($connect,$_POST['hiddencounter']); /* PREVENT A BIT OF SQL INJECTION */
for($i=0;$i<$hiddencounter;$i++){ /* FOR LOOP BASED ON THE NUMROWS BELOW */
if(!empty($checkbox[$i])){ /* IF THE CHECKBOX IS TICKED */
$del_id=mysqli_real_escape_string($connect,$checkbox[$i]); /* PREVENT A BIT OF SQL INJECTION */
$sql2="DELETE FROM threads WHERE id='$del_id'";
$query2=mysqli_query($connect,$sql2); /* IMPLEMENT THE DELETE QUERY */
}
} /* END OF FOR LOOP */
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
} /* END OF ISSET DELETE */
?>
<form action="" method="POST" > <?php /* YOU CAN LEAVE THE ACTION BLANK, TO SUBMIT THE FORM ON THE PAGE ITSELF */ ?>
<table border="1" width="400" cellpadding="0" cellspacing="0">
<tr>
<td>#</td>
<td>id</td>
<td>subject</td>
</tr>
<?
$sql="SELECT * FROM threads ORDER BY id DESC";
$query=mysqli_query($connect,$sql);
$num=mysqli_num_rows($query);
/* SUBMIT THE NUMBER OF ROWS QUERIED THROUGH HIDDEN INPUT */
echo "<input type='hidden' name='hiddencounter' value='$num'>";
while($row=mysqli_fetch_array($query)){ /* FETCH DATA */
?>
<tr>
<td><input type="checkbox" name="checkbox[]" value="<?php echo $row['id'] ?>"></td>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['topic'] ?></td>
</tr>
<?
} /* END OF WHILE LOOP $QUERY */
?>
<input type="submit" name="delete" value="delete" >
<?php
mysqli_close($connect);
?>
</table>
</form>
Explanation why your code did not work:
- Your
isset($delete)
: where does$delete
variable came from? - Your for loop doesn't determine if the checkboxes submitted has values or not.
- Your
<meta>
redirect. It redirects to delete.php after the for loop, so that is the reason you don't see any errors. But I'm pretty sure you would see some errors if you remove the<meta>
redirect. - Even if the delete is working, you wouldn't see the result right after because the delete query is after your fetching of data.