bindiya Posted December 15, 2010 Share Posted December 15, 2010 I have a webpage where all the database items are displyes in a table format.The table also has a check box.Upon clicking the delete button i need to delete all the items whish has the checkbox checked. How will i do that Link to comment https://forums.phpfreaks.com/topic/221750-need-a-php-code-to-delete-selected-items-of-database-items-using-checkbox/ Share on other sites More sharing options...
MMDE Posted December 15, 2010 Share Posted December 15, 2010 You want to use several optional checkboxes at the same time... If you don't want to do some huge javascript workaround, you will have to check for each one of those checkboxes... if it varies how many of them there is, then you specify that as a hidden input. I'm thinking: $minchecks=0; $maxchecks=50; if(!empty($_POST['totalchecks'])){ if($_POST['totalchecks']>$minchecks && $_POST['totalchecks']<$maxchecks){ for($i=0;$i<$_POST['totalchecks'];$i++){ if(!empty($_POST['check'.$i])){ $checked[]=$_POST['check'.$i]; // this number can be faked, so make sure you check it } } } } print_r($checked); echo '<form action="" method="post"> $i=0; foreach($array AS $row){ echo '<input type="checkbox" name="check'.$i.'" value="'.$row.'" />'; $i++; } echo '<input type='hidden' name="totalchecks" value="'.$i.'" /> </form> sorry if I did a typo or anything, and sure probably some better way of doing this... just an idea. Link to comment https://forums.phpfreaks.com/topic/221750-need-a-php-code-to-delete-selected-items-of-database-items-using-checkbox/#findComment-1147624 Share on other sites More sharing options...
AbraCadaver Posted December 15, 2010 Share Posted December 15, 2010 Easier with arrays: <input type="checkbox" name="checkbox[]" value="1"/> <input type="checkbox" name="checkbox[]" value="2"/> <input type="checkbox" name="checkbox[]" value="3"/> $values = implode(',', array_map('mysql_real_escape_string', $_POST['checkboxes'])); $query = "DELETE FROM table_name WHERE id IN ($values)"; Link to comment https://forums.phpfreaks.com/topic/221750-need-a-php-code-to-delete-selected-items-of-database-items-using-checkbox/#findComment-1147747 Share on other sites More sharing options...
Psycho Posted December 15, 2010 Share Posted December 15, 2010 Easier with arrays: <input type="checkbox" name="checkbox[]" value="1"/> <input type="checkbox" name="checkbox[]" value="2"/> <input type="checkbox" name="checkbox[]" value="3"/> $values = implode(',', array_map('mysql_real_escape_string', $_POST['checkboxes'])); $query = "DELETE FROM table_name WHERE id IN ($values)"; Definitely agree with that approach, but... You have a typo. The field names are "checkbox[]", but the PHP code is referencing them as $_POST['checkboxes']. Besides, naming a field "checkbox" (or "checkboxes") is poor implementation. Always give elements/variables descriptive names. Also, I am hyper-sensitive to preventing errors. The code above uses 'mysql_real_escape_string', but the values passed "should" be integers. If a non-integer is passed the query would fail. You could either do more intensive validating of the values, or just use intval() to ensure the values are interpreted as integers. Definitely several options you could go with here. Updated/corrected: <input type="checkbox" name="delete_ids[]" value="1"/> <input type="checkbox" name="delete_ids[]" value="2"/> <input type="checkbox" name="delete_ids[]" value="3"/> $values = implode(',', array_map('intval', $_POST['delete_ids'])); $query = "DELETE FROM table_name WHERE id IN ($values)"; Link to comment https://forums.phpfreaks.com/topic/221750-need-a-php-code-to-delete-selected-items-of-database-items-using-checkbox/#findComment-1147763 Share on other sites More sharing options...
AbraCadaver Posted December 15, 2010 Share Posted December 15, 2010 mjdamato, I agree. Normally, unless I'm actually posting code that is meant to be usable as is to solve a problem/error then my code is meant to be illustrative only. Rather than bang out a bunch of code for someone to use, I normally just want to give them an example or something to push them in the right direction. Link to comment https://forums.phpfreaks.com/topic/221750-need-a-php-code-to-delete-selected-items-of-database-items-using-checkbox/#findComment-1147772 Share on other sites More sharing options...
bindiya Posted December 16, 2010 Author Share Posted December 16, 2010 thank u for the code.It works well ,if i check more than one checkboxes.But if i check for only one checkbox it gives me errors. what will be the reason? the error is Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/content/77/6911777/html/admin/themes/view_res.php on line 87 Warning: implode() [function.implode]: Invalid arguments passed in /home/content/77/6911777/html/admin/themes/view_res.php on line 87 Link to comment https://forums.phpfreaks.com/topic/221750-need-a-php-code-to-delete-selected-items-of-database-items-using-checkbox/#findComment-1147982 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.