Jump to content

PLEASE HELP - foreach Error I Think


Morty222

Recommended Posts

I ask the user how many players are on their roster and they select the answer from a drop down menu, click submit and it generates 1 roster spot for the amout of players they selected. Here is this code:

 

*** $players comes from the number they selected above ***

<?php
$i=1;
while($i<=$players){
echo "<tr><td><input name=player[$i][fname] type=text size=15/></td><td><input name=player[$i][lname] type=text size=15 /></td><td><input name=player[$i][jersey] type=text size=5 /></td><td><input name=player[$i][dob] type=text size=7 /></td><td><input name=player[$i][address] type=text size=15 /></td><td><input name=player[$i][city] type=text size=15 /></td><td><input name=player[$i][zipcode] type=text size=5 /></td><td><input name=player[$i][position] type=text size=15 /></td></tr>";
$i++;
}
?>

///This just generates rows of form fields that a coach can fill out to submit their roster.

 

 

 

 

When I do a submit to add each player to the database using a foreach loop all the data in every field says Array, not what they typed in.

 

I either have something wrong in the fields above or my foreach is messed up. Here is my foreach to add them to the DB:

 

<?php
$teamID = $_GET['teamID'];

foreach($key as $c=> $value){

mysql_query("insert into tbl_rosters (teamID, fname, lname, jersey, dob, address, city, zipcode, position) VALUES ('$teamID', '".$_POST['fname']."','".$_POST['lname']."', '".$_POST['jersey']."', '".$_POST['dob']."', '".$_POST['address']."', '".$_POST['city']."', '".$_POST['zipcode']."', '".$_POST['position']."') ") or die(mysql_error());

} 
?>

 

(edited by kenrbnsn to add


tags)

Link to comment
https://forums.phpfreaks.com/topic/127263-please-help-foreach-error-i-think/
Share on other sites

Pleas post your code in the code tags. It makes it much easier to read.

 

It looks like you're not accessing the array properly. You foreach loop is looping through the "$key" array, but you're not listing where you declare $key. As an example of properly accessing the arrays, you have:

<input name=player[$i][fname] type=text size=15/>

To access that using $_GET, you would use this:

$_GET['player'][$i]['fname']

 

Or, to loop through all the "player" variables:

<?php
$players = $_GET['player'];
foreach ($players as $i=>$ar){
  foreach ($ar as $key=>$val){
    // whatever...
  }
}
?>

Archived

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

×
×
  • 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.