Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.