Forum Moderators: coopster
Checkbox code:
<input name='checkbox[]' type='checkbox' id='checkbox[]' value=\"".$row['id']."\">
Browser output:
<input name='checkbox[]' type='checkbox' id='checkbox[]' value="392">
Button at the bottom of form:
<input name='delete' type='submit' id='delete' value='Delete Multiple Selected Items'>
if($delete){
//for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful refresh page
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=checkbox.php\">";
}
mysql_close();
The problem might be with having 2 "for" loops since the other loops through and creates the pagination. I have tried changing all instances of $i to something else like $a and that didn't work. In all the things I have tried to change, the selected id's still aren't being deleted. I commented out the loop in this last statement, but it just refreshes the page checkbox.php The above code works in the page that is a mixture of php and html, but I may be missing something. Let me know if you need anymore code. I kept trying to work with this and read up on things but I cannot get it working. Thank you for any help.
Try echo/print $sql as shown below and see if you are getting the valid queries.
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
echo $sql;
$result = mysql_query($sql);
}
}
Once again you helped to provide a solution. Thank you very much.
I was missing the...
$delete = $_REQUEST['delete'];
$checkbox = $_REQUEST['checkbox'];
$count = count($_REQUEST['checkbox']);
which wasn't used in the tutorial I was looking at since it was coded a different way. Since it was html and php mixed, it was in the following format.
php to connect to db
html to label columns
php while statement to retrieve data
html mixed with php that echo'ed the db results
<?php }?> to close the while
php to delete entries
html to end the table and form
I also did some reading up on $_REQUEST to understand how it works and it's purpose. Thank you again for the help.