Forum Moderators: coopster

Message Too Old, No Replies

deleting multiple database items with checkbox

         

php4U

4:05 am on Nov 21, 2007 (gmt 0)

10+ Year Member



With the script I am building I thought it might be nice for a user to be able to delete multiple database items at one time. I found a tutorial that combined html and php to do this, but I am trying to incorporate it into my existing page...because I worked a while to get the pagination the way I wanted. (credit to d40sithui)

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.

Habtom

4:51 am on Nov 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have the following set?
$delete = $_REQUEST['delete'];
$checkbox = $_REQUEST['checkbox'];
$count = count($_REQUEST['checkbox']);

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);
}
}

dreamcatcher

3:09 pm on Nov 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why do you have the name and the id set as 'checkbox[]'. Only the name needs to be shown with the array operator.

Also, you can do it without a loop:


if (!empty($_POST['checkbox'])) {

mysql_query("DELETE FROM $tbl_name WHERE id IN(".implode(",",$_POST['checkbox']).")";

}

dc

php4U

7:03 pm on Nov 21, 2007 (gmt 0)

10+ Year Member



Habtom,

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.

php4U

7:09 pm on Nov 21, 2007 (gmt 0)

10+ Year Member



Thanks dreamcatcher,

I was in reply mode, and didn't see your post. It is at least working now, but I will check out what you suggested since it may improve what I have. That is the nice thing about php and this board, many ways of doing things and a willingness of users to help.