View Full Version : Whats wrong with this code?

08-23-2006, 03:21 AM
I was trying to insert but unfortunately the error occurs.

Warning: Invalid argument supplied for foreach() in /srv/www/htdocs/pre_req.php on line 74

foreach($subject as $subs)

mysql_query("INSERT into pre_req(prosno,sub_code,pre_requisite) VALUES('$prosno','$subs','$preqs[$i]')");

} [/FONT][/FONT]

08-23-2006, 04:08 AM
foreach requires an array, which, evidently, your variable $subject is not.

08-23-2006, 01:56 PM
Strange thing about arrays is that it only becomes an array when multiple values are inside it. A variable declared an array, with only 1 value in it, is seen a scalar variable in PHP.

So you might wanna try a check like count(array), or sizeof(array) before entering the foreach.

On a sidenote, I hope you put only part of the code here, since I would expect a loop somewhere surrounding the filling of $subject...