Jump to content


Photo

foreach error in checkbox form


  • Please log in to reply
2 replies to this topic

#1 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 02 April 2006 - 02:13 AM

I'm trying to make a simple form that will allow users to select certain baseball players to add to their profiles.

Here is part of my form:

<form name="myplayers" method="post" action="insertmyplayers.php">
<?php
echo "<TABLE width=\"100%\" border=\"0\">";
echo "<TD align=\"center\" valign=\"top\"><TABLE width=\"100%\" border=\"0\"><TD><div align=\"center\"><div
align=\"center\"><h1>Catcher</div></h1>";

$query = "SELECT ps.*,p.*, t.*
        FROM playerstats ps
        INNER JOIN
        (
            players p INNER JOIN mlbteams t
            ON t.mlbteam_id = p.mlbteam_id
        )
          ON ps.player_id = p.player_id
        AND ps.year = 2005
        AND position_id = 1
                ORDER by lname";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
    $pid =  $row['player_id'];
    $lname = $row['lname'];
    $fname = $row['fname'];
echo "<input name=\"myplayers[];\" type=\"checkbox\" value=\"$pid\">$lname, $fname<br>";

}
echo "</TD></TABLE></TD>";

My post page that executes the insert query:

foreach($_POST['player_id'] AS $newp) {

    $sql = "INSERT INTO myplayers (player_id) VALUES ($newp)";
    mysql_query ($sql) or die(mysql_error()); 
}

?>

I keep getting this error on submission:

Warning: Invalid argument supplied for foreach() in

If I select 5 players for the form page. This is what the array looks like: print_r($_POST);

Array ( [myplayers] => Array ( [0] => 676 [1] => 677 [2] => 1078 [3] => 1080 [4] => 1089 [5] => 1090 ) [player_id] => )

Any idea what I'm messing up

#2 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 02 April 2006 - 03:08 AM

Figured it out

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 April 2006 - 04:24 AM

Please post your solution so that people reading your post who have the same problem can learn.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users