Jump to content

Delete multiple entries w/ Checkboxes


Zoey

Recommended Posts

I've got this page in which people can input their suggestions, and the information is stored on my server. To deal with these suggestions, I created a page (with the help of my friend Bill) which allows me to delete/edit suggestions. However, currently, the page will only delete one suggestion at a time.. but I'd like it to be able to delete all of the suggestions that are checked. Here's the code for the admin actions

[code]
    $row_del = (isset($_POST['sub_del'])) ? $_POST['sub_del'] : 0;
    if(isset($_POST['sub_del']) || isset($_POST['sub_edit'])) {
        foreach($_POST as $key => $value) {
            if ( strpos($key, "id_") !== false ) {
                $row_id = str_replace("id_","",$key);
                if ($row_id > 0) break;
            }
        }
[/code]

sub_del is the button you click to delete entries.. sub_edit is the name of the button you click to edit entries.

Here's the code that actually deletes the entries

[code]
else if($row_del) {
    foreach ($_POST as $id => $value) {
          $query = "DELETE FROM polls WHERE id=$row_id"; }
[/code]

And here's the code used to make the checkboxes that allow me to edit/delete a suggestion.

[code]
$maindiv.= "<input type='checkbox' name='id_".$row['id']."'>";
[/code]

Does anyone have any suggestions on how I can get the script to allow multiple suggestions to be deleted at once? Thanks.

(If you need me to post more of the coding, just let me know.. I only posted what I considered the important snipits, but since this code isn't entirely mine, there could be more that I'm missing... thanks).
Link to comment
https://forums.phpfreaks.com/topic/5246-delete-multiple-entries-w-checkboxes/
Share on other sites

You are almost there!!!!

now....

your checkbox...use it lie this instead.

[code]
$maindiv.= '<input type="checkbox" name="id[" . $row['id'] . "]">';
[/code]

then in the code that deletes these commenst do this....

[code]
// create array of comments to delete.
$i = 0;
foreach ($_POST['id'] as $key => $value { // loop through all the post vars id
$delarr .= ($i ==0) ? $value : "," . $value; // create a comma separated list of comment id's to delete.
$i = 1;
}

// Once you have the list just do this...
$query = "DELETE FROM polls WHERE id IN (" . $delarr . ")";
// echo $query; // remove the comment obefore the echo to check the query you will run.
// $query = mysql_query($query); // remove the comment at the start of this line when you are happy.
[/code]

Thats about as elegant as you will get it.
Ack, ok nevermind. I thought I had it, but for some reason I'm having another problem. I changed the code so that the checkbox code is

[code]$maindiv.= "<input type='checkbox' name='ids[]' value='".$row['id']."'>";[/code]

And to delete multple comments is:

[code]else if($row_del) {
    $query = "DELETE FROM polls WHERE";
    $j=0;
    foreach($_POST['ids'] as $key=>$id)
      {
          if($j>0)
          {
            $query .= " OR";
          }
      $query .= " id='".$id."'";
      $j++;          
     }
    }[/code]

But now its not letting me edit comments.

I think it has something to do with the remaining:

[code]
    if(isset($_POST['sub_del']) || isset($_POST['sub_edit'])) {
        foreach($_POST as $key => $value) {
            if ( strpos($key, "id_") !== false ) {
                $row_id = str_replace("id_","",$key);
                if ($row_id > 0) break;
            }
        }
    } [/code]

since the checkbox is no longer called id_.. but no matter what combination of ids[] I try to change the id_ to, its still not allowing me to edit my comments.

Any idea what I should fix the id_ to?

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.