TecTao Posted June 25, 2007 Share Posted June 25, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/ Share on other sites More sharing options...
trq Posted June 25, 2007 Share Posted June 25, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282424 Share on other sites More sharing options...
pocobueno1388 Posted June 25, 2007 Share Posted June 25, 2007 EDIT: Beat to it 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282427 Share on other sites More sharing options...
TecTao Posted June 25, 2007 Author Share Posted June 25, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282434 Share on other sites More sharing options...
trq Posted June 25, 2007 Share Posted June 25, 2007 The main thing is the value would be the member ID? Correct. The example you posted seems logical. Link to comment https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282436 Share on other sites More sharing options...
trq Posted June 25, 2007 Share Posted June 25, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282438 Share on other sites More sharing options...
TecTao Posted June 25, 2007 Author Share Posted June 25, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282441 Share on other sites More sharing options...
trq Posted June 25, 2007 Share Posted June 25, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282455 Share on other sites More sharing options...
teng84 Posted June 25, 2007 Share Posted June 25, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282457 Share on other sites More sharing options...
TecTao Posted June 26, 2007 Author Share Posted June 26, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282525 Share on other sites More sharing options...
pocobueno1388 Posted June 26, 2007 Share Posted June 26, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282545 Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 I do have to agree with her her? Im a dude dude. Link to comment https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282550 Share on other sites More sharing options...
TecTao Posted June 26, 2007 Author Share Posted June 26, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282559 Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 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 https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282564 Share on other sites More sharing options...
pocobueno1388 Posted June 26, 2007 Share Posted June 26, 2007 I do have to agree with her her? Im a dude dude. Ah! Sorry Thorpe, I knew that. I really did xP Link to comment https://forums.phpfreaks.com/topic/57157-using-a-check-box-for-a-mass-delete/#findComment-282583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.