I've been working on a small admin panel that should allot me to delete multiple items useing check boxes. The problem is once it hits my foreach() statment it craps out and gives me this warning
Warning: Invalid argument supplied for foreach() in C:\adminpanel\administration.php on line 66

I've tried about 5 or 6 different ways of doing it but they all require a loop and I always end up getting the same type of warning. Heres my current code, can someone please explain how I should fix this. Much thanks.

html part:

<b>Del:</b><input type='checkbox' name='checkrem[]' value='<? echo $row['id']; ?>' /><a href='?crem'>Del</a>

php part:

elseif(isset($_GET['crem'])  && (isset($_SESSION['news_login']))) {
$rem = $_REQUEST['checkrem[]']; // $checkrem in the array
//  print_r($rem);
foreach ($rem as $gone) {
 mysql_query("DELETE FROM `headlines` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."' LIMIT 1") or die ("Fail to remove");}
}

    $checkrem is passed as an entire array... So call the variable as is, and don't add the square brackets.

    elseif(isset($_GET['crem'])  && (isset($_SESSION['news_login']))) { 
    $rem = $_GET['checkrem']; // $checkrem in the array 
    //  print_r($rem); 
    foreach ($rem as $gone) { 
    mysql_query("DELETE FROM `headlines` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."' LIMIT 1") or die ("Fail to remove");} 
    } 
    

    Also, why are you bothering to loop through this if each iteration of the loop calls the $_GET['id'] which would be the same for each loop regardless of what the value of $gone is?

      I made the changes but still get the error: Warning: Invalid argument supplied for foreach() on the same line as before. 🙁

      Well theres a list of items and when I check them there value will be the mysql id for them. I want to remove all the checked boxes...what would be a better way of removing all the checked boxes then?

        I would start by doing a print_r($checkrem); to see what's being passed by the form because that error is basically saying your $rem array is not an array (more than likely)... Also, rem is probably not on the list of reserved words, but you might want to rename that particular variable just in case... (I'm thinking about the DOS REM command)

        Next, given what you are saying I believe your query would be something more like:
        DELETE FROM headlines WHERE id = '".$gone ."' LIMIT 1

          TheDefender wrote:

          I would start by doing a print_r($checkrem); to see what's being passed by the form because that error is basically saying your $rem array is not an array (more than likely)... Also, rem is probably not on the list of reserved words, but you might want to rename that particular variable just in case... (I'm thinking about the DOS REM command)

          Next, given what you are saying I believe your query would be something more like:
          DELETE FROM headlines WHERE id = '".$gone ."' LIMIT 1

          K, I tried the fallowing and $checkrem returned nothing.

          elseif(isset($_GET['crem'])  && (isset($_SESSION['news_login']))) {
          print_r($checkrem);
          $srem = $_GET['checkrem']; // $checkrem in the array
          //  print_r($rem);
          print_r($checkrem);
          //foreach ($srem as $gone) {
          //DELETE FROM `headlines` WHERE `id` = '".$gone ."' LIMIT 1
          // mysql_query("DELETE FROM `headlines` WHERE `id` = '".mysql_real_escape_string($gone)."' LIMIT 1") or die ("Fail to remove");}
          }
          

          Heres the html again.

          <input type='checkbox' name='checkrem[]' value='<? echo $row['id']; ?>' /><a href='?crem'>Del</a> 

            Wait, it just dawned on me... you're using the link right after the input box. What's the point of the check box if you have the link? Also, the link won't help you with multiple checkboxes.... Why not submit a form using a button at the bottom of the list? (like when deleting multiple emails using these online email progs)

              well my list is relativity large so I figured id just put a url next to each so I didn't have to go all the way to the top or bottom. I guess maybe I should add a button to the bottom :/

                Ahhh, consider pagination to break the list down into multiple pages worth of data, or... you could put a delete button every20 rows or so...

                To do what you are trying to do, you'd either need to also include the ID in the link itself, or you'd haev to use a Javascript onClick event withint the link to submit the form...

                  then how would I do that with the id in the url?

                    I ended up solving it on my own. I did the following.

                    HTML

                     <form method="post" action="administration.php?crem">
                    			<b>Del:</b><input type='checkbox' name='checkrem[]' value='<? echo $row['id']; ?>' /> 
                    <input type="submit" value="Click"/>

                    PHP

                    elseif (isset($_POST['checkrem']) && (isset($_SESSION['news_login']))) {
                    
                    foreach ($_POST['checkrem'] as $headline) {
                        mysql_query("DELETE FROM `headlines` WHERE `id` = '" . mysql_real_escape_string($headline) . "' LIMIT 1") or die ("Fail to remove");
                    }
                    
                    }

                    Everything works now.

                      Write a Reply...