PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old July 23rd, 2002, 04:27 PM
AaaDee AaaDee is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Huddersfield, West Yorkshire, UK
Posts: 4 AaaDee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to AaaDee
Insert Multiple Data into a table

I am having difficulties.

Is it possible to Insert multiple data into a database table at one time?

an example

two columns

Beckham Man UTD
Owen Liverpool
Henry Arsenal

Thats what my page looks like.

I want to insert all three of them lines into the database in one table.

But all it does at the moment is inserts

Henry Arsenal

it does the last line all the time.

Pweese Help

Reply With Quote
  #2  
Old July 23rd, 2002, 04:53 PM
greggory greggory is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Reims, France
Posts: 82 greggory User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Insert Multiple Data into a table

No, U have to query three times in your case ...

But you can write a loop

php Code:
Original - php Code
  1.  
  2. <?php
  3. $my_array = Array(
  4.  "Beckham" => "Man UTD"
  5.  "Owen" => "Liverpool"
  6.  "Henry" => "Arsenal"
  7. );
  8.  
  9.  
  10.  
  11. while ( list( $name, $team ) = each( $my_array )) {
  12.    mysql_query( "insert into my_table values '$name', '$team' );
  13. }
  14. ?>

Reply With Quote
  #3  
Old July 23rd, 2002, 11:06 PM
Taoism Taoism is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Winnipeg, MB, Canada
Posts: 81 Taoism User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 sec
Reputation Power: 2
RE: Insert Multiple Data into a table

You could also write it as a single insert:

INSERT INTO <table> VALUES ("Beckham","Man UTD"),("Owen","Liverpool"),("Henry","Arsenal");

This type of insert will be faster if you have alot of data to insert.

Cheers,
Keith.

Reply With Quote
  #4  
Old July 24th, 2002, 09:57 AM
AaaDee AaaDee is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Huddersfield, West Yorkshire, UK
Posts: 4 AaaDee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to AaaDee
RE: Insert Multiple Data into a table

Hi,

Because i am getting the details from the database, this makes my problem a little bit difficult.

On the page where it displays, Beckham Man UTD, Owen Liverpool, Henry Arsenal. It doesnt actually say there names,

the code is
echo "
<tr>
<td>Player</td>
<td>Club</td>
</tr>";

while ( $row = mysql_fetch_array( $sql_result ) )

{

$player = $row["player"];
$club = $row["club"];

echo "

<tr>
<td>$player</td>
<td>$club</td>
</tr>";

So this get all the players and clubs in the database. So i dont actually say player club three times, just once.

im looking for something like

if $num_rows = number of rows in DB

then insert * into table

How would i go about this please.

Cheers

Reply With Quote
  #5  
Old July 24th, 2002, 11:55 AM
CmdrDats CmdrDats is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: <br><img src='http://www.dats.co.za/icon.gif'>
Posts: 269 CmdrDats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Send a message via ICQ to CmdrDats Send a message via AIM to CmdrDats Send a message via Yahoo to CmdrDats
RE: Insert Multiple Data into a table

This should work :
php Code:
Original - php Code
  1.  
  2. <?
  3. <tr>
  4. <td>Player</td>
  5. <td>Club</td>
  6. </tr>";
  7.  
  8. while ( $row = mysql_fetch_array( $sql_result ) ) {
  9.    
  10.     $player = $row["player"];
  11.     $club = $row["club"];
  12.    
  13.     echo "
  14.    
  15.     <tr>
  16.     <td>$player</td>
  17.     <td>$club</td>
  18.     </tr>";
  19.    
  20.     $table = "players";
  21.    
  22.     mysql_query("INSERT INTO $table (player, club) VALUES ('$player', '$club')");
  23. }
  24. ?>

You just need to modify it a little though, to connect and all that stuff.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Insert Multiple Data into a table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway