PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 5th, 2003, 10:43 AM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
php inserting multiple records into a table

all right I am trying to take some records from one table and insert them into another table. I am using MySql as the database and here is the code:

$sql = "SELECT * FROM cart WHERE username = '$_SESSION[username]'";
$result = @mysql_query($sql, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$ProdNum[] = $row['ProdNum'];
$qty[] = $row['qty'];

foreach($ProdNum as $id=>$value){
$vProdNum = $value;
$vqty = $qty[$id];
$sql = "INSERT INTO orderitem (ItemNum, QtyOrd, ProdNum, OrderNum) VALUES (Null, '$vqty', '$vProdNum', '$OrderNum')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
}
}

please please please help me because I am getting tired of looking at this script

Reply With Quote
  #2  
Old December 5th, 2003, 11:31 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Are you getting an error message?

Reply With Quote
  #3  
Old December 5th, 2003, 12:11 PM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
no errors but only the last record is showing up in the table

Reply With Quote
  #4  
Old December 5th, 2003, 01:32 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
How're you figuring you're getting an array out of your database? You're trying to put a string value from the database into $ProdNum[] and then iterate through $ProdNum, which isn't an array to begin with.

Reply With Quote
  #5  
Old December 5th, 2003, 03:55 PM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
and that is why I am asking for help because I don't have a f*cking clue what I am doing!!!!!!!!

Reply With Quote
  #6  
Old December 5th, 2003, 04:42 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Would it make more sense to do something like this?

PHP Code:
 $sql "SELECT * FROM cart WHERE username = '$_SESSION[username]'";
$result = @mysql_query($sql$connection) or die(mysql_error());
while (
$row mysql_fetch_array($result)) {
     
$sql "INSERT INTO orderitem (ItemNum, QtyOrd, ProdNum, OrderNum) VALUES (Null, '" $row["qty"] . "', '" $row["ProdNum"] . "', '" $row["OrderNum"] . "')";
     
$result = @mysql_query($sql$connection) or die(mysql_error());


Reply With Quote
  #7  
Old December 5th, 2003, 05:21 PM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
yes that would actually make a lot more sense but it is still overriding the first record so the last record is the one that shows up in table

Reply With Quote
  #8  
Old December 6th, 2003, 08:49 AM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 15
is it just one record that shows up...im sorry but i dont get what u mean by just the last record shows up

Reply With Quote
  #9  
Old December 6th, 2003, 10:20 AM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
the last record is overwriting the previous records.. I think I need some sort of for statment

Reply With Quote
  #10  
Old December 6th, 2003, 10:40 AM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 15
so in other words only the last record gets inserted into the database

Reply With Quote
  #11  
Old December 6th, 2003, 01:55 PM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
yes

Reply With Quote
  #12  
Old December 6th, 2003, 05:26 PM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
so if anyone could help me I would really appreciate it. this is for a school project and I've been working on this particular script for about a week now and I am not getting anywhere with it.

Reply With Quote
  #13  
Old December 8th, 2003, 08:12 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Is your ItemNum an auto_increment field? The only thing I can think of that might be happening is that it's not an auto_increment field and that you're just inserting NULL as the value over and over again. Still, a new record should go in each time. How exactly are you determining that only the last row is being inserted?

Reply With Quote
  #14  
Old December 8th, 2003, 08:19 AM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
ItemNum is an autoincrement
well I get to choose which records go into the cart table and then I am trying to insert those same records into the orderitem table so I know exactly what is going into that table and what isn't for that matter

Reply With Quote
  #15  
Old December 8th, 2003, 10:45 AM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
if anyone else has the same problem, here is what you do

$sql ="INSERT INTO table2 (attr1,attr2,attr3,etc) SELECT table1.attr1,table1.attr2,table1,attr3 FROM table1 WHERE table1.attr3 >100";
$result = @mysql_query($sql, $connection) or die(mysql_error());

Reply With Quote
  #16  
Old December 8th, 2003, 10:46 AM
gallifray gallifray is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Pennsylvania
Posts: 16 gallifray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 3 sec
Reputation Power: 0
thanks for trying to help me :-)

Reply With Quote
  #17  
Old April 28th, 2009, 12:43 PM
allozysob allozysob is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2009
Location: Switzerland
Posts: 7 allozysob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 44 sec
Reputation Power: 0
Send a message via ICQ to allozysob
Allo!

Como estas :P ! Je lit pas mal le forum souvent… mais là je decide de m'abonner pour vous demander c'est quoi l'adresse web au juste du magasine de decouvertes de chalets . J'ai apprit qu'il y avait une couple de videos à faire sur ce site..! thank you!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > php inserting multiple records into a table


Developer Shed Advertisers and Affiliates


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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap