Reputation: 23
I'm having a problem with deleting data using checkbox. I actually, retrieve all the records that I want to show/display. But the Delete button doesn't work.
$sql="SELECT * FROM admin";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
<?php
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]' value='".$rows['id']."'/>" . "</td>";
echo "<td>" . $rows['username'] . "</td>";
echo "<td>" . $rows['email'] . "</td>";
echo "</tr>";
?>
<input name="delete" type="submit" id="delete" value="Delete">
Here's the IF STATEMENT to check if the delete button was active.
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM admin WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_admin.php\">";
}
}
Any help is appreciated. Thank you!
Upvotes: 2
Views: 142
Reputation: 143
Try do it:
if(isset($_POST['delete']) { //check if you received the post
for($i=0; $i<count($_POST['check']); $i++) { // count the elements
$del_id = $_POST['check'][$i];
...
Upvotes: 0
Reputation: 2071
You can IN
function of MySql
if($delete){
$checkboxIds = implode(',',$_POST['checkbox']);
$sql = "DELETE FROM admin WHERE id IN ('".$checkboxIds."')";
$result = mysql_query($sql);
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_admin.php\">";
}
}
Upvotes: 1