...

View Full Version : Whats wrong with this code?



che_anj
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

if(isset($_POST['save']))
{
$preqs=$_POST['preq'];
$subject=$_POST['subcode'];
$i=0;
foreach($subject as $subs)
{

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


$i++;
}
} [/FONT][/FONT]

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

4xz
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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum