Jump to content

Warning: Invalid argument supplied for foreach()


jonlim

Recommended Posts

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));

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

Archived

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

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