Jump to content


Photo

Inserting dynamicaly created rows into mysql?


  • Please log in to reply
4 replies to this topic

#1 cardoso

cardoso
  • Members
  • Pip
  • Newbie
  • 8 posts
  • LocationToronto

Posted 09 March 2003 - 02:03 PM

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

#2 kippi

kippi
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 26 March 2008 - 02:22 PM

hi Nelson 

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

Many Thanks

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 26 March 2008 - 05:21 PM

"multiple records with the same field name" == what does this mean?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 rhodesa

rhodesa
  • Staff Alumni
  • Advanced Member
  • 5,254 posts
  • LocationBoston, MA

Posted 26 March 2008 - 05:26 PM

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
  }
?>

Aaron Rhodes
PHP Developer


#5 aschk

aschk
  • Staff Alumni
  • Advanced Member
  • 1,245 posts

Posted 27 March 2008 - 10:54 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users