Forum Moderators: coopster

Message Too Old, No Replies

inserting multiple records from 1 form. how?

         

geffchang

2:31 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



i have a page that lets you input a number which is passed to a php script that generates rows of textboxes / records (first name, last name). how do i pass these rows / records into the database?

>>
index.htm
>>
textbox: input number of records to insert ___3___

>>
script.php
first _______ last ___________
first _______ last ___________
first _______ last ___________

do i loop like..? or how should it be?
<?php
$number = $_GET['number'];
for ($i = 0; $i <= $number; $i++)
{
echo"<input type=text name=first" . $i . ">";
echo"<input type=text name=last" . $i . "> <br>";
}
?>

now, how about passing this to the database? help please.

CaseyRyan

3:48 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



On the code that is outputted as the form, I would have a hidden input which keeps the number that they entered. In your example, you would put:

<input type=hidden name=number value=<? echo $number;?>>

Now that you've stored the number, when they fill out the form and click submit, you can take the number, make a similar loop and read through each of the input variables. It might look something like this:

<?php  
$number = $_GET['number'];
for ($i = 0; $i <= $number; $i++)
{
$first = $_GET['first'.i];
$last = $_GET['last'.i];
/* Do the Insert Here */
}
?>

Another way to do it would be to not add the number to the end of the first and last inputs names. When the form is filled out and submitted, you would end up with 2 arrays (first and last) that you could loop through with php. They will both be of the same length so you could loop through one and pull from both. For this to work, you could add the hidden input (number) like above or you can probably get the number from the array itself.

-=casey=-