Jump to content

[SOLVED] Help Processing Form Values as Array


limitphp

Recommended Posts

I have a form that will submit x number of people.

textboxes:

name=fname[]

name=lname[]

name=position[]

 

The x could be 1 person, 2 or 7 people, etc....

I'll have that number sent in the form also via a textbox name=num

 

For each person I'll need to add their fname, lname and position to a database.

 

How can I process this form as an array?

 

I could do the:

foreach($_POST['fname'] AS $fname)
   echo $fname;

 

but then I'd have to do that loop for each item....fname, lname and position....is there a more effiecient way?

 

thanks

 

 

Yes using one loop not 3. You must make sure that all arrays are of the same size

for($x = 0; $x < count($_POST['fname']); $x++) {
  mysql_query("INSERT INTO table SET fname='".mysql_real_escape_string($_POST['fname'][$x])."', lname='".mysql_real_escape_string($_POST['lname'][$x])."', position='".mysql_real_escape_string($_POST['position'][$x])."'");
}

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.