Jump to content

Archived

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

predator

why am i getting problems with this foreach loop

Recommended Posts

hey i got this for each loop

if($_POST['remove']){
foreach($_POST['remove'] as $k=>$v){
mysql_query("DELETE FROM sections WHERE id='$v' LIMIT 1");
}
}

and i am getting the error of

Warning: Invalid argument supplied for foreach()

the $_POST['remove'] is an array i get from an html page and it has a products id assigned to it

but i just cant work out why this is giving me an error.

any help greatly apprechiated

Regards
Mark

Share this post


Link to post
Share on other sites
If you are getitng that error then $_POST['remove'] is not an array. Try this code:
[code=php:0]echo '<pre>' . print_r($_POST['remove'], true) . '</pre>';[/code]

What does that return?

Also if you have input fields that have the same name as remove make sure you have add the square brackets after the name, eg: remove[]

Share this post


Link to post
Share on other sites
Are you sure it is an array? Make sure it has [] after the name in HTML, and use is_array in PHP to double check.

Share this post


Link to post
Share on other sites
yes it is an array the name of each check box is
remove[{$content[i].id}]

where $content is obviously the id that will be passed into each remove element.

personally i dont like this way of coding however i have taken this project over at work and cant totally re write the code as the customer needs it done obviously sooner than later

regards
Mark

Share this post


Link to post
Share on other sites
it would still be nice to see the input's HTML to verify that it isn't a spelling or notation error.

Share this post


Link to post
Share on other sites

×

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.