|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
Are you getting an error message?
|
|
#3
|
|||
|
|||
|
no errors but only the last record is showing up in the table
|
|
#4
|
||||
|
||||
|
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.
|
|
#5
|
|||
|
|||
|
and that is why I am asking for help because I don't have a f*cking clue what I am doing!!!!!!!!
|
|
#6
|
||||
|
||||
|
Would it make more sense to do something like this?
PHP Code:
|
|
#7
|
|||
|
|||
|
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
|
|
#8
|
|||
|
|||
|
is it just one record that shows up...im sorry but i dont get what u mean by just the last record shows up
|
|
#9
|
|||
|
|||
|
the last record is overwriting the previous records.. I think I need some sort of for statment
|
|
#10
|
|||
|
|||
|
so in other words only the last record gets inserted into the database
|
|
#11
|
|||
|
|||
|
yes
|
|
#12
|
|||
|
|||
|
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.
|
|
#13
|
||||
|
||||
|
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?
|
|
#14
|
|||
|
|||
|
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 |
|
#15
|
|||
|
|||
|
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()); |
|
#16
|
|||
|
|||
|
thanks for trying to help me :-)
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > php inserting multiple records into a table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|