Jump to content

foreach error in checkbox form


esiason14

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">
<?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>";[/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());
}

?>[/code]

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.