SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problems with foreach

    I'm having a little trouble the following script to work...

    Here's the Form:
    Code:
    <form action="insert.phtml" method="post">
    <SELECT NAME="imagelinks[]" MULTIPLE SIZE="8">
        <OPTION VALUE="100">None</OPTION>
        <OPTION VALUE="2">zxcv</OPTION>
        <OPTION VALUE="4">asdf</OPTION>
    </SELECT> 
    <input type="hidden" name=model value="88kx500">
    <input type="submit">
    </form>

    Here's the Insert script:
    Code:
    	foreach ($imagelink as $value) {
    		$mValues.="($value),";
    	}
    	$mValues = substr($mValues,0,-1); // remove last comma
    	$sql = "INSERT INTO imagelinks (model, imagelink) VALUES ('$model', '$mValues')";
    	echo $sql;
    	//$result = mysql_query($sql);
    Here's the Error:

    Warning: Invalid argument supplied for foreach() in insert.phtml on line 14
    INSERT INTO imagelinks (model, imagelink) VALUES ('88kx500', '')

    Line 14 is the foreach line.

    Your help is appreciated!
    - Mike
    http://www.georgiaoffroad.com

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if this matters, but your form field is called "$imagelinks[]", and your foreach loop uses "$imagelink[]". without the "s" at the end.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wish that were the problem, I put the sample form in my example as the real one is generated with javascript, and is complex. It does pass an array to the sql script ($imagelink[])
    - Mike
    http://www.georgiaoffroad.com

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its quite obvious that your javascript is not operating properly, did you try and print out the count() of the array on the sql page.

    Something like

    PHP Code:
    <?
    if(count($imagelink) > 0) {
        foreach (
    $imagelink as $value) {
            
    $mValues.="($value),";
            }
        }
    else {
        print 
    "My form is not passing an array properly";
        }    
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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