jonlim Posted August 19, 2009 Share Posted August 19, 2009 Hi guys, encountered the "Warning: Invalid argument supplied for foreach()" error and will appreciate some help. Error on each of the "foreach" line, therefore 3 errors in total. Thanks! foreach ($fields as $key => $value){ if($value == '') unset($fields[$key]); } // generate SQL statement $SQL = "INSERT INTO `" . $table . "` ("; foreach($fields as $key => $value) $SQL .= "`". $key . "`, "; $SQL = substr($SQL, 0, strlen($SQL) - 2); $SQL .= ") VALUES ("; foreach($fields as $key => $value) $SQL .= sprintf("%s, ", quote_smart($value)); $SQL = substr($SQL, 0, strlen($SQL) - 2) . ")"; return(mysql_query($SQL)); Quote Link to comment https://forums.phpfreaks.com/topic/170971-warning-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
Garethp Posted August 19, 2009 Share Posted August 19, 2009 print_r($fields); Try it Quote Link to comment https://forums.phpfreaks.com/topic/170971-warning-invalid-argument-supplied-for-foreach/#findComment-901757 Share on other sites More sharing options...
JonnoTheDev Posted August 19, 2009 Share Posted August 19, 2009 $fields must be an array! Quote Link to comment https://forums.phpfreaks.com/topic/170971-warning-invalid-argument-supplied-for-foreach/#findComment-901758 Share on other sites More sharing options...
jonlim Posted August 19, 2009 Author Share Posted August 19, 2009 print_r($fields); Try it Thanks Garethp. That didn't work though Quote Link to comment https://forums.phpfreaks.com/topic/170971-warning-invalid-argument-supplied-for-foreach/#findComment-901898 Share on other sites More sharing options...
JonnoTheDev Posted August 19, 2009 Share Posted August 19, 2009 That wasn't a fix. print_r() prints the content of an array. Again if $fields is not an array you will receive the error you have when trying to use in a foreach() loop http://uk2.php.net/manual/en/control-structures.foreach.php Quote Link to comment https://forums.phpfreaks.com/topic/170971-warning-invalid-argument-supplied-for-foreach/#findComment-901905 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.