Jump to content

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
   }

}

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/
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.

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655502
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>");
					}

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655509
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.

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655514
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

 

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655521
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.

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655522
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 ???

 

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655547
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 ??

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655552
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.

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655558
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

 

 

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655572
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.

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655574
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

Link to comment
https://forums.phpfreaks.com/topic/126732-hi-guys-and-girls/#findComment-655582
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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