Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 06-16-2004, 02:40 AM   #1 (permalink)
debbieau
Registered User
 
Join Date: Jun 2004
Posts: 2
debbieau is on a distinguished road
Would be really grateful for help with inserting multiple records

I would be very grateful for some help. I am new to PHP and need to insert insert multiple records into one table.

I have a form where each row is one record.

I am guessing I need to do some sort of loop, but really do not know which.


Could anyone please advise me the easiest way to do this.

Thank you
debbieau is offline   Reply With Quote
Old 06-16-2004, 05:13 AM   #2 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Depending on your form, it might be easy to use the handy array syntax.

eg:


Code:
<!-- your html form - excuse the shoddy form -->

<form ACTION="this.php" METHOD="POST">

<table>

<tr>
  <td><input type="text" size="10" name="name[]"></td>
  <td><input type="text" size="2" name="age[]"></td>
</tr>

<tr>
  <td><input type="text" size="10" name="name[]"></td>
  <td><input type="text" size="2" name="age[]"></td>
</tr>

</table>

</form>
So we have two rows both prompting for name and age. In php, just loop through the array and insert.

PHP Code:

if (sizeof($name) > 0) {
   
$db = new DB//however you get your db object.
   
foreach ($name as $k => $v) {
      
$q  = <<<EOQ
      INSERT INTO table_name (name_field, age_field)
      VALUES (
         '
{$name[$k]}',
         '
{$age[$k]}'
      )
EOQ;

      
$db->query($q);

   }
}
else {
   echo 
'No data!! ahh';


If you didn't use the array var syntax in your form, you could still do something similar, but it might be more difficult. eg: name your vars with the row number then do a for() loop for the number of rows you have.

-r
idx is offline   Reply With Quote
Old 06-16-2004, 04:06 PM   #3 (permalink)
debbieau
Registered User
 
Join Date: Jun 2004
Posts: 2
debbieau is on a distinguished road
Thank you so much. This is a great help and very clear. I do appreciate it.
debbieau is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble inserting multiple records into table, Please someone help me! Thanks bearslife PHP 3 05-14-2004 05:59 PM


All times are GMT -8. The time now is 09:56 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting