Jump to content

[SOLVED] multiple checkbox deleting


coupe-r

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/155998-solved-multiple-checkbox-deleting/
Share on other sites

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

$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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.