coupe-r Posted April 28, 2009 Share Posted April 28, 2009 I am trying to delete multiple items using check boxes. Everything is OK except when I select more than 1. if(isset($_POST['delete_btn'])) { foreach($_POST['checkbox'] as $value) { $checked = $value $query1 = "DELETE FROM tickets WHERE t_id = '".$checked."'"; $query2 = "DELETE FROM problems WHERE t_id = "'. $checked.'""; } } I am echoing out the SQL statements and no matter what, it only deletes the first box I select. I can append ( .= ) any / all of the vars in the loops, but still deletes 1 record? Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/ Share on other sites More sharing options...
Cosizzle Posted April 28, 2009 Share Posted April 28, 2009 I don't think you need to append the $checked. Are your check boxes in an array? Also just for testing purposes what happens if you run this? <?php if(isset($_POST['delete_btn'])) { foreach($_POST['checkbox'] as $value) { echo $value; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821195 Share on other sites More sharing options...
coupe-r Posted April 28, 2009 Author Share Posted April 28, 2009 When I echo $value, it only reads the last check box I clicked. if you have check boxes 1 - 10, it will echo 10 Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821199 Share on other sites More sharing options...
Cosizzle Posted April 28, 2009 Share Posted April 28, 2009 what does your <input> look like? should be something like: for ($i = 0; $i < 10; $i++) { echo "<input type='checkbox' name='myValues[]' value='$i'>"; } Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821210 Share on other sites More sharing options...
coupe-r Posted April 28, 2009 Author Share Posted April 28, 2009 while($row=mysql_fetch_array($result)) <input type="checkbox" name="checkbox[]" id="checkbox" value="'.$ticket.'"/> Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821214 Share on other sites More sharing options...
Cosizzle Posted April 28, 2009 Share Posted April 28, 2009 and whats $ticket? or the value of it? whats it look like if you look at the source (webpage online) sorry for the silly questions... this is how i'd debug this. (Had a similar problem recently that i got working... eventually) Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821242 Share on other sites More sharing options...
coupe-r Posted April 28, 2009 Author Share Posted April 28, 2009 $ticket is the ticket_ID number. Viewing page source shows ...... value="25" This is perfect. Value is 25 for the check box beside ticket 25 on the screen. When I try to delete 2 or more tickets at the same time, it only deletes 1, the first one. Deleting 1 at a time works, but a pain in the ass. Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821247 Share on other sites More sharing options...
Cosizzle Posted April 28, 2009 Share Posted April 28, 2009 and your executing the queries outside the foreach loop right? Is it possible to execute them within the foreach loop? @mysql_query($query1); @mysql_query($query2); Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821252 Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 if(isset($_POST['delete_btn'])) { $checked = array(); foreach($_POST['checkbox'] as $value) { $checked[] = $value } $query1 = "DELETE FROM tickets WHERE t_id IN (" . implode(", ", $checked) . ")"; $query2 = "DELETE FROM problems WHERE t_id IN (" . implode(", ", $checked) . ")"; } That should delete all checked records with ease. Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821253 Share on other sites More sharing options...
coupe-r Posted April 28, 2009 Author Share Posted April 28, 2009 Still only deletes 1 row within the site, however... Now, I pasted the SQL statement in mySQL, it deletes 1 row, but I get an error as well. "Truncated incorrect DOUBLE value:'32, 33, 34' " Not sure what this is. Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821273 Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 if(isset($_POST['delete_btn'])) { $checked = array(); foreach($_POST['checkbox'] as $value) { $checked[] = intval($value); } $query1 = "DELETE FROM tickets WHERE t_id IN (" . implode(", ", $checked) . ")"; $query2 = "DELETE FROM problems WHERE t_id IN (" . implode(", ", $checked) . ")"; } Try that and see. Also check what type the t_id field is. If it is a double, it will expect a double not an INT. And should it be a double? If the t_id field is an INT field then the above should work by converting the value coming from the form into an integer. Quote Link to comment https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/#findComment-821276 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.