Jump to content

Deleting multiple rows from mysql table in php using checkboxes


ggw

Recommended Posts

Hi,

I am having difficulty deleting rows in my table using check boxes. I have all the check boxes displaying for each row and a delete button below the table. However when i click on the delete button it isnt deleting the checked rows, i have displayed the code below that i am using;

 

 <?php 
	  



$result = mysql_query("SELECT * FROM contact ORDER BY msg_id ASC");

echo "<table border='1'>
<tr>
<th>Delete</th>
<th>Message ID</th>
<th>Name</th>
<th>Email</th>
<th>Message</th>

</tr>";

while($row = mysql_fetch_array($result))
  {
  ?>
   <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['del_id']; ?>"></td>
   <?php
  echo "<td>" . $row['msg_id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['msg'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>


<?php
// Check if delete button active, start this 
if(isset($_GET['delete']))	{

for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM contact WHERE id=".$_GET['del_id'];
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php 
if($result){

}
}
mysql_close();
?>

 

Please help

Thank you

Link to comment
Share on other sites

For the checkbox name, you'll want to have an array.

 

<input type="checkbox" name="delete_these[]" value="your id goes here" />

 

Then you can do

$ids = implode(', ', $_GET['delete_these']);

$sql = "DELETE FROM table WHERE id IN($ids)";

Link to comment
Share on other sites

I think they have been implemented correctly

 

<form>
          <?php 
	  



$result = mysql_query("SELECT * FROM contact ORDER BY msg_id ASC");

echo "<table border='1'>
<tr>
<th>Delete</th>
<th>Message ID</th>
<th>Name</th>
<th>Email</th>
<th>Message</th>

</tr>";

while($row = mysql_fetch_array($result))
  {
  ?>
   <td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['del_id']; ?>"></td>
   <?php
  echo "<td>" . $row['msg_id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['msg'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>


<?php
// Check if delete button active, start this 
if(isset($_GET['delete']))	{

for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$ids = implode(', ', $_GET['delete_these']);
$sql = "DELETE FROM contact WHERE msg_id IN($ids)";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php 
if($result){

}
}
mysql_close();
?>
</form>

Link to comment
Share on other sites

replace these two lines

$ids = implode(', ', $_GET['delete_these']);

$sql = "DELETE FROM contact WHERE msg_id IN($ids)";

 

with

print_r($_GET['delete_these']);

$ids = implode(', ', $_GET['delete_these']);

$sql = "DELETE FROM contact WHERE msg_id IN($ids)";

echo "<br />SQL: $sql<br />";

 

I'm curious what it says.

 

Edit: Just noticed, you need to do the deleting before you select and display, otherwise you won't see your changes until the next page view.

Also make your form valid with a method and action. http://www.htmlhelp.com/reference/html40/forms/form.html

Link to comment
Share on other sites

Sorry it didn't say anything because my server has not been refreshing, I have added this code in so the variable is set

$count=mysql_num_rows($result)

 

It now says (when selecting 2 check boxes to delete);

Array ( [0] => [1] => )

SQL: DELETE FROM contact WHERE del_id IN(, )

Array ( [0] => [1] => )

SQL: DELETE FROM contact WHERE del_id IN(, )

Array ( [0] => [1] => )

SQL: DELETE FROM contact WHERE del_id IN(, )

Array ( [0] => [1] => )

SQL: DELETE FROM contact WHERE del_id IN(, )

 

 

I think it would of said before if my server had been refreshing

sorry

Link to comment
Share on other sites

<input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['del_id']; ?>">

 

Is there really a column named del_id in your table? I think that should be the msg_id (the primary key on the table). Because it looks like your $_GET['delete_these'] array is full of empty values.

 

Link to comment
Share on other sites

Assuming as DavidAM pointed out you DO have a primary column in your table called del_id then you could do this.  Note processing really should be done above <html> and output sent to browser.

<?php
// Check if delete button active, start this 
if(isset($_POST['delete']))	{
$ids = implode(', ', $_POST['delete_these']);
$sql = "DELETE FROM table WHERE id IN($ids)";
$result = mysql_query($sql);

if($result){
header("location: delete_multiple.php");
exit;
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
$result = mysql_query("SELECT * FROM contact ORDER BY msg_id ASC");
echo "<form action=\"\" method=\"post\">";
echo "<table border='1'>
<tr>
<th>Delete</th>
<th>Message ID</th>
<th>Name</th>
<th>Email</th>
<th>Message</th>  
</tr>";
while($row = mysql_fetch_array($result))
  {
echo "<tr>";
echo "<td align=\"center\" bgcolor=\"#FFFFFF\"><input name=\"delete_these[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"{$row['del_id']}\"></td>";
echo "<td>" . $row['msg_id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['msg'] . "</td>";
echo "</tr>";
}
echo "<tr>";
echo '<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>';
echo "</tr>":  
echo "</table>";
echo "</form>";
?> 
</body>
</html>

Link to comment
Share on other sites

You do not want to (and shouldn't be) running multiple queries for the DELETE operations. If you are passing the IDs to be deleted in an array you simply need to validate/sanitize those values and then run ONE delete query

 

Example

//Assign all the ids to a variable. The array_map() with Intval assures
//they are integers. The array_filter() removes invalid values
$deleteIDs = array_filter(array_map('intval', $_POST['delete_ids']));

//Implode delete IDs into string for query
$deleteIDsSQL = implode(', ', $deleteIDs);
//Create one query to delete all records at once
$query = "DELETE FROM table_name WHERE ID IN ({$deleteIDsSQL})";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.