ghqwerty Posted October 2, 2008 Share Posted October 2, 2008 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 } } Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 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>"); } Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 ok i will fix is that comment explainin the implode thing ??? Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 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 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 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 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 Where is the form tag, make sure that's present somewhere and that form data is actually being posted. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 le form tag <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 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 ??? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 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 ?? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 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 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 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 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 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. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 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 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 Try removing the quotes around the ('$delete') When I tested a similar script on my localhost it worked fine. Quote Link to comment Share on other sites More sharing options...
ghqwerty Posted October 2, 2008 Author Share Posted October 2, 2008 works like a beuty thanks for the help and the patience Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 2, 2008 Share Posted October 2, 2008 No worries! Glad it's working. Good luck with future development. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.