Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder che_anj's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    157
    Thanks
    12
    Thanked 3 Times in 3 Posts

    Whats wrong with this code?

    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]

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    foreach requires an array, which, evidently, your variable $subject is not.

  • #3
    4xz
    4xz is offline
    Regular Coder
    Join Date
    Aug 2004
    Location
    localhost
    Posts
    163
    Thanks
    0
    Thanked 1 Time in 1 Post
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •