PDA

View Full Version : Inserting Mutiple Rows Into DB


Pee-H-Pee
04-09-2006, 05:01 AM
I am trying to add a row into my database for each item my customers purchase. The code I have below works as long as there is only one item purchased. But if there are more than one it only adds the first item.

I know there has to be a foreach function in there somewhere and that is where I get lost.

I use SecureNetShop shopping cart:

Each item's name, description, price and quantity are posted in the following format:
item1=name~price~quantity
item2=name~price~quantity and so on...

// Insert ordered items into items table

list($order_num, $item_name, $item_price, $item_qty) =
split("~", $item1);

mysql_query("INSERT into items
(inv_num, item_name, item_qty, item_price)
VALUES
('$inv_num','$item_name', '$item_qty', '$item_price')");

goughy000
04-09-2006, 09:48 PM
data.txt

item1=name~price~quantity
item2=name~price~quantity


insert.php

<?php
$datafile = file("data.txt");

foreach($datafile AS $line){
$itemarray = explode("~", $line);
list($order_num, $item_name, $item_price, $item_qty) = $itemarray;
mysql_query("INSERT into items (inv_num, item_name, item_qty, item_price) VALUES ('$inv_num','$item_name', '$item_qty', '$item_price')");
}
?>


www.php.net/foreach (http://www.php.net/foreach)
www.php.net/file (http://www.php.net/file)

Pee-H-Pee
04-09-2006, 10:02 PM
Thanks for the reply... but the items data is posted on the final checkout page (thank you page) of the cart and is not a data.txt file.

So I am a little confused about this line:
$datafile = file("data.txt");