Jump to content


This topic is now archived and is closed to further replies.


foreach error in checkbox form

Recommended Posts

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:

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

$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>";[/code]

My post page that executes the insert query:

[code]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:

[code]Warning: Invalid argument supplied for foreach() in[/code]

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

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

Any idea what I'm messing up

Share this post

Link to post
Share on other sites
Please post your solution so that people reading your post who have the same problem can learn.


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.