Jump to content

Archived

This topic is now archived and is closed to further replies.

ghqwerty

hi guys and girls

Recommended Posts

im new here got recommended by a friend of my dads

 

could some one please help me with a problem

 

im getting this error

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\inventory.php on line 57

 

line 57 = 

foreach($deleted as $dId){

 

the section of code its in =

 

if(sizeof($deleted)){  
   foreach($deleted as $dId){
       $delete = mysql_query("delete from items where itemid = ".(int)$dId."") or die('crap');  //Adding an (int) will help avoid SQL errors
   }

}

Share this post


Link to post
Share on other sites

Instead of sizeof() I would suggest isset().

Make sure that $deleted is an array. That error occurs when you supply a foreach() loop with a string rather then an array. Basically anything that isn't an array will throw that error. You can ensure that it is an array by using is_array(). But if it is always supposed to be an array then something else is not working correctly.

Share this post


Link to post
Share on other sites

hmm still not working

 

what it is part of is an inventory page where there are checkboxes for each item you own and a delete button and im trying to get it so that when you click the delete button everything that is selected is deleted

 

here is the whole code for you

 


					$deleted = $_GET['delete'];
					if(isset($deleted)){  
					    foreach($deleted as $dId){
					        $delete = mysql_query("delete from items where itemid = ".(int)$dId."") or die('crap');  //Adding an (int) will help avoid SQL errors
					    }
					}

					$result = mysql_query("select itemid, item from items where userid = ".(int)$_SESSION['id']) or die('summit went wrong'); 
					$count = mysql_num_rows($result);  
					if($count <1){  
					    print("<tr><td colspan=\"3\"><center>You currently have no items.<center></td></tr>");  
					}else{  
					    while($item = mysql_fetch_array($result)){  
					        print("<tr><td><center>".$item['item']."</center></td><td width=\"40%\"><center>".$item['itemid']."</center></td><td width=\"10$\">  
					        <input type=\"checkbox\" name=\"delete[]\" value=".$item['itemid'].">   
					        </td></tr>  
					        ");  
					    } 
							print("<tr><td colspan='3'><p align='right'><input type='submit' name='delete' id='delete' value='delete!'</p></td></tr>");
					}

Share this post


Link to post
Share on other sites

Well make sure that the form's method is set to GET if you want the values to appear in the address bar. I would suggest that you use POST instead then you can do something like this:

 

if(isset($_POST['delete'])){
/*
Now we change it from this:
	Array (1,2,5,8,12)
To this:
	1,2,5,8,12
So it becomes a string, then we can use IN
*/
$delete = implode(",", $_POST['delete']);

//Now run query
$qry = mysql_query("DELETE FROM items WHERE itemid IN ($delete)") or die(mysql_error());
}

 

But I really suggest using the "post" method.

Share this post


Link to post
Share on other sites

getting this error

 

Warning: implode() [function.implode]: Bad arguments. in C:\xampp\htdocs\inventory.php on line 64

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

 

Share this post


Link to post
Share on other sites

Yes, just explaining the use of the implode() function. :)

It just changes it from an array to a string, the opposite of explode().

 

The error would most certainly be that it is not being passed as an array properly.

 

Place this somewhere on the page:

 

print_r($_POST['delete']);

 

And see if it prints an array. If it isn't, check your input, something is going wrong somewhere.

Share this post


Link to post
Share on other sites

ive done that but nothing is being printed

 

 

<input type=\"checkbox\" name=\"delete[]\" value=".$item['itemid'].">

 

thats where the array SHOULD be being formed i think

Share this post


Link to post
Share on other sites

Where is the form tag, make sure that's present somewhere and that form data is actually being posted.

Share this post


Link to post
Share on other sites

le form tag

 

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Share this post


Link to post
Share on other sites

this is what i get when i echo out $_POST['delete'];

 

delete!

 

 

:S wierd  now im officially confused

 

has got to be something to do with this

 

<input type=\"checkbox\" name=\"delete[]\" value=".$item['itemid'].">

#

 

possibly something to do with the delete[] ???

 

or woul it be the value ???

 

Share this post


Link to post
Share on other sites

Ah I didn't notice that. Change the name of the submit button to submit or change the name of the checkbox to "delete_check". Then you will need to change the $_POST['delete'] with $_POST['delete_check']

 

Silly me. :)

Share this post


Link to post
Share on other sites

Warning: implode() [function.implode]: Bad arguments. in C:\xampp\htdocs\inventory.php on line 64

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

 

same mf'n warning

 

what does

 

delete_check do ??

Share this post


Link to post
Share on other sites

Okay, before you had two inputs called the same thing, so the second one (the submit button) was overwriting the first one (the checkboxes). So instead of an array being sent it was sending the string "delete!"

So by renaming it to delete_check you need to rename all other instances of $_POST['delete'] with $_POST['delete_check'] where you want the checkbox values to be used.

Share this post


Link to post
Share on other sites

ok so been experimenting by echoing out a few things and have found that if i select 1 item and click delete i still get

say it is item id3

Warning: implode() [function.implode]: Bad arguments. in C:\xampp\htdocs\inventory.php on line 64

line 64

$delete = implode(",", $delete2);

$delete2 is

$delete2 = $_POST['delete_check'];

then delete_check = 3

 

however if i select 1 and 3 it will = 3 or 3 and 4 will be 4

 

somewhere its not being put into an array

 

 

Share this post


Link to post
Share on other sites

Okay, here is a small example of how it should be working. I don't know what stage your code is at and its all jumbled in the topic so take a look at this:

 

<?php
if(isset($_POST['submit'])){
$delete = $_POST['delete_checkbox'];

if(!empty($delete)){
	//The array is not empty, so we can perform the implode function on it
	$delete = implode(",", $delete);
	$sql = "DELETE FROM table WHERE id IN ('$delete')";

	//Show the SQL, instead you would perform the query here.
	echo $sql;
}
}else{
echo <<<html
<form action="" method="post">
	<input type="checkbox" name="delete_checkbox[]" value="1" /> #1<br />
	<input type="checkbox" name="delete_checkbox[]" value="3" /> #3<br />
	<input type="checkbox" name="delete_checkbox[]" value="4" /> #4<br />
	<input type="checkbox" name="delete_checkbox[]" value="7" /> #7<br />
	<input type="submit" name="submit" value="Delete!" />
</form>
html;
}
?>

 

It's the basic idea behind it and how it should be working.

Share this post


Link to post
Share on other sites

i understand that however the problem lies in the fact that i am not producing a set number of checkboxes

 

    while($item = mysql_fetch_array($result)){  
					        print("<tr><td><center>".$item['item']."</center></td><td width=\"40%\"><center>".$item['itemid']."</center></td><td width=\"10$\">  
					        <input type=\"checkbox\" name=\"delete_check\" value=".$item['itemid'].">   
					        </td></tr>  
					        ");  
					    } 

 

if you look it will provide a checkboxe at the end of each row of data selected from the database

Share this post


Link to post
Share on other sites

Okay you changed the name but forgot the []. They're important as they create it into an array.

 

<input type=\"checkbox\" name=\"delete_check[]\" value=".$item['itemid'].">

 

You need to look at my example closely. :)

Share this post


Link to post
Share on other sites

ive done that however if i select more than one it only deletes the first one however the error has gon and now it does say array

Share this post


Link to post
Share on other sites

Try removing the quotes around the ('$delete')

When I tested a similar script on my localhost it worked fine.

Share this post


Link to post
Share on other sites
:) No worries! Glad it's working. Good luck with future development.

Share this post


Link to post
Share on other sites

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