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)); 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 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! 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 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 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
Archived
This topic is now archived and is closed to further replies.