Jump to content

Archived

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

cardoso

Inserting dynamicaly created rows into mysql?

Recommended Posts

Hi,

 

I have a form where I dynamically create fields using PHP and a while loop. Basically, I display:

swimmer name Field | Pace Field | Time Field | Date Field

swimmer name Field | Pace Field | Time Field | Date Field

swimmer name Field | Pace Field | Time Field | Date Field etc...

 

one line for every swimmer in the table.

 

I want to insert all of these records into a mysql table when they hit submit. I know how to do this with one record but I\'m new and have never done this with multiple records with the same field name.

 

Can you please help me out and please provide a small example as I have a feeling I have to use arrays... and I\'m new at this.

 

 

Thank you so much!

Nelson

Share this post


Link to post
Share on other sites

hi Nelson 

 

Did you ever find away to do this? this is what i am looking for,

 

Many Thanks

Share this post


Link to post
Share on other sites

"multiple records with the same field name" == what does this mean?

Share this post


Link to post
Share on other sites

your form elements should get [] added to them like so:

 

<input type="text" name="name[]" /><input type="text" name="pace[]" /><input type="text" name="time[]" /><input type="text" name="date[]" /><br>
<input type="text" name="name[]" /><input type="text" name="pace[]" /><input type="text" name="time[]" /><input type="text" name="date[]" /><br>
<input type="text" name="name[]" /><input type="text" name="pace[]" /><input type="text" name="time[]" /><input type="text" name="date[]" /><br>
<input type="text" name="name[]" /><input type="text" name="pace[]" /><input type="text" name="time[]" /><input type="text" name="date[]" /><br>

 

then in your PHP:

 

<?php
  for($n=0;$n < count($_POST['name']);$n++){
    $name = $_POST['name'][$n];
    $pace = $_POST['pace'][$n];
    $time = $_POST['time'][$n];
    $date = $_POST['date'][$n];
    //Now do you insert
  }
?>

Share this post


Link to post
Share on other sites

If you have PDO you can use prepared statements to help MySQL with this. And you only have to change the bind variables on each loop.

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.