[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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());
            }
      }
0
pingeyeg
Asked:
pingeyeg
2 Solutions
 
nplibCommented:
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. TudorPHP 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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now