Jump to content

Using a Check Box for a Mass Delete


TecTao

Recommended Posts

In a membership, we want to be able to mass delete certain duplicate and unwanted members.  Was thinking of a checkbox type deletion sort of like what I've seen on myspace.

 

a list of members would fill a page in rows.  each member has a unique member number.  On the left would be a check box and at the bottom a buttom to delete all the the checked members.

 

How would I pass the checked members id number to the delete page and the query to delete all the members where the m_id = all the id numbers

 

Thanks in advance for any help.

mike

Link to comment
Share on other sites

How would I pass the checked members id number to the delete page

 

Through the value attribute of each checkbox. So you might have something like....

 

<form action="process.php" method="post">
  <input type="checkbox" name="delete[]" value="1" />
  <input type="checkbox" name="delete[]" value="4" />
  <input type="checkbox" name="delete[]" value="6" />
  <input type="submit" name="submit">
</form>

 

Then, on process.php.

 

<?php

    $sql = "DELETE FROM members WHERE m_id IN('" . implode("','",$_POST['delete']) . "')";

?>

Link to comment
Share on other sites

EDIT: Beat to it  :P

 

Give the current code you have, then we can help you from there.

 

Basically you need to have your checkbox named to accommodate arrays. So name it something like "checked[]", then for the value your going to want it to be the unique column so you know which row to delete.

 

<input type='checkbox' name='checked[]' value='{$row['unique_col']}'>

 

Then to get which checkboxes are selected you need to call this code after you check if the submit button was clicked.

 

<?php

if ($_POST['submit']){ //check if they submitted

   //see which check boxes were selected
   foreach ($_POST['checked'] as $key => $val){

      //delete records
      mysql_query("DELETE FROM table WHERE unique_col='$key'")or die(mysql_error());
   }
}

?>

 

     

 

 

Link to comment
Share on other sites

Since the list of members is dynamically generated, for example all the members with names that start with the letter A, I assume the list would be with in the form and the value is the unique member number sort of like:

 

<form action="process.php" method="post">

  <input type="checkbox" name="delete[]" value="<? $m_id ?>" />, <?echo m_name  ?>, <? echo m_phone ?>

  <input type="submit" name="submit">

</form>

 

where what is displayed is

check box (value of member id), member name, member phone

 

The main thing is the value would be the member ID?

Link to comment
Share on other sites

 

Then to get which checkboxes are selected you need to call this code after you check if the submit button was clicked.

 

<?php

if ($_POST['submit']){ //check if they submitted

   //see which check boxes were selected
   foreach ($_POST['checked'] as $key => $val){

      //delete records
      mysql_query("DELETE FROM table WHERE unique_col='$key'")or die(mysql_error());
   }
}

?>

 

Running seperate queries for each checkbox (within a loop) would be a very inificient method of achieving this. The method I psted above gets it all done in one simple query without any need for loops.

Link to comment
Share on other sites

Here's the basic code on the submit page:

 

<?php

$result = mysql_query( "SELECT * FROM dealer_list WHERE TRIM(d_name) LIKE '$letter%' ORDER BY d_name ASC" )

or die("SELECT Error: ".mysql_error());

$num_rows = mysql_num_rows($result);

?>

 

<table width=770 align=center border=1 cellspacing=0 cellpadding=2>

 

<? php

  while ($row = mysql_fetch_array($result))

  {

    extract($row);

?>

 

<form name="form1" method="post" action="delete.php">

 

  <tr>

    <td width="252" >

    <input type="m_id" name="m_id" value="<? echo"$d_id"?>">

    </td>

    <td width="252" ><? echo"$d_id"?></td>

    <td width="195" ><? echo"$d_name"?></b><br>

<? echo"$d_address"?><br>

    <? echo"$d_state"?>,  <? echo"$d_zip"?></td>

    <td width="303" >

web site: <? echo"$d_url"?><br>

email: <? echo"$d_email"?></td>

  </tr>

<input name="submit" type="submit" value="Submit">

</form>

 

<?

$row_count++;

}

 

mysql_close;

?>

Link to comment
Share on other sites

You don't wont a new form for each user. Ive also cleaned up a fair bit of your code, though theres still more you should do.

 

<?php 
$result = mysql_query( "SELECT * FROM dealer_list WHERE TRIM(d_name) LIKE '$letter%' ORDER BY d_name ASC" )
or die("SELECT Error: ".mysql_error());
?>
<table width=770 align=center border=1 cellspacing=0 cellpadding=2>
<form name="form1" method="post" action="delete.php">
<?php
  while ($row = mysql_fetch_array($result))
  {
     extract($row);
?>
  <tr>
    <td width="252" >
    <input type="checkbox" name="delete[]" value="<?php echo $d_id ?>">
    </td>
    <td width="252" ><?php echo $d_id ?></td> 
    <td width="195" ><?php echo $d_name ?>

    <?php echo $d_address ?>

    <?php echo $d_state ?>,  <?php echo $d_zip ?></td>
    <td width="303" >
   web site: <?php echo $d_url ?>

   email: <?php echo $d_email ?></td>
  </tr>

<?php  
}
mysql_close;
?>
<input name="submit" type="submit" value="Submit">
</form>

Link to comment
Share on other sites

 

Then to get which checkboxes are selected you need to call this code after you check if the submit button was clicked.

 

<?php

if ($_POST['submit']){ //check if they submitted

   //see which check boxes were selected
   foreach ($_POST['checked'] as $key => $val){

      //delete records
      mysql_query("DELETE FROM table WHERE unique_col='$key'")or die(mysql_error());
   }
}

?>

 

Running seperate queries for each checkbox (within a loop) would be a very inificient method of achieving this. The method I psted above gets it all done in one simple query without any need for loops.

 

^^^^ long process try this and edit if theres error but its the right way

mysql_query("DELETE FROM table WHERE unique_col IN(implode(',',$_POST['checked'])))or die(mysql_error());

 

 

Link to comment
Share on other sites

Thanks for all the imput.

 

After a bit of trial and error, I realized that the variable being passed from the submit page was working but it wasn't deleting the anything.  I finally realized that in the code

 

<?php

if ($_POST['submit']){ //check if they submitted

   //see which check boxes were selected
   foreach ($_POST['checked'] as $key => $val){

      //delete records
      mysql_query("DELETE FROM table WHERE unique_col='$key'")or die(mysql_error());
   }
}

?>

 

the where clause should read Where unique_col=$val and not $key.

 

Thanks for all the help.

m

Link to comment
Share on other sites

Oops, sorry about that little error. Is there a reason your not using Thorpe's code? I do have to agree with her that it is more efficient, were you having problems getting it to work?

 

Either way works, so if your happy with the code I supplied, then go ahead and mark the topic as solved :)

 

 

 

Link to comment
Share on other sites

lol...

are you talking about the first comment by thorpe.  tried it but it didn't work then saw yours pocobueno and then thorpe re-posted with a similar code on the delete page.  i have to admin that after studying both, i couldn't understand the first but did in concept understand the delete code the both of you suggested.  and then it was just fiddling around with it.

 

thanks to both of you.

m

Link to comment
Share on other sites

If this works...

 

<?php

if ($_POST['submit']) {
   foreach ($_POST['checked'] as $val) {
      mysql_query("DELETE FROM table WHERE unique_col='$val'") or die(mysql_error());
   }
}

?>

 

Then this will work more efficiently.

 

<?php

  if (isset($_POST['submit'])) {
    mysql_query("DELETE FROM table WHERE unique_col IN('" . implode("','", $_POST['checked']) . "')") or die(mysql_error());
  }

?>

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.