Trying to use a foreach statement to delete all selected items

I am trying to delete items in the database using a foreach statement, but am having a tough time understanding the foreach statement.  This is what I have so far, but I am getting the error:

Warning: Invalid argument supplied for foreach()


if($_POST['dID']) {
            foreach($_POST['dID'] AS $dID) {
                  $deleteItem = sprintf("DELETE
                        FROM category_items
                        WHERE id=%d", $dID);
                  $itemDeleted = mysql_query($deleteItem) or die("Delete did not work because: " . mysql_error());
            }
      }
LVL 1
pingeyegAsked:
Who is Participating?
 
nplibConnect With a Mentor Commented:
Sounds like $_POST['dID'] is not an array.

if your using check boxes, in the name field add [] to the name, this will make that value into array

<input type="checkbox" name="dID[]' value="true" />

etc.
0
 
Ionut A. TudorConnect With a Mentor PHP ProgrammerCommented:
Gives you error because its not an array, see below how to check if its array or not

<?php
 
$dID = $_POST['dID'];
if(isset($dID)) {
			
			if(is_array($dID)){
				echo "use foreach";
				}
			else{
				echo "don't use foreach as this is not an array";
			}
      }
 
?>

Open in new window

0
 
agamalCommented:
have you tried "al3sc12" solution yet ????? waiting your reply
0
 
pingeyegAuthor Commented:
Sorry, got side tracked.  Yes, and it worked great!  Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.