Jump to content


Photo

Insering Multiple Rows In DB


  • Please log in to reply
5 replies to this topic

#1 Chet

Chet
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 April 2006 - 06:46 PM

Hello,

I am using the SecureNetShop shopping cart for my business. Their final checkout page (thank you page) posts the items as such (for silent posting):

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

So if the customer purchases 3 items it would post like this:

item1=Mens Polo Shirt~25.00~2
item2=Ladies Sport Socks~10.00~3
item3=Mens Tie~20.00~1

I am trying to post a row in my DB for each item purchased but I am lost when it comes to arrays or loops.

My code below works great as long as the customer only purchases one item. But if they purchase 2 or more items it only inserts the first item instead of all of them.

// Insert all 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')");

If I change item1 to item2 it will insert the second item on the list only. If I change it to item3 it will insert only the third product on the list and so on... So I need to get it to insert all items purchased.

If anyone can help me out here I would greatly appreciate it :)

Thank You

#2 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 09 April 2006 - 06:59 PM

its very simple
just put your codes inside a function like insertrow();

then call that function number of times with items like insertrow(item1); insertrow(item2) ... insertrow(itemN);

i hope you can got my idea? enough?

Thank you,
Karthi Keyan.

www.karthi.us

#3 Chet

Chet
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 April 2006 - 07:06 PM

[!--quoteo(post=363088:date=Apr 9 2006, 02:59 PM:name=Karthikeyan)--][div class=\'quotetop\']QUOTE(Karthikeyan @ Apr 9 2006, 02:59 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]i hope you can got my idea? enough?[/quote] Thanks for replying... but no, I don't understand :)


#4 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 09 April 2006 - 07:28 PM

[!--quoteo(post=363091:date=Apr 9 2006, 02:06 PM:name=Chet)--][div class=\'quotetop\']QUOTE(Chet @ Apr 9 2006, 02:06 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Thanks for replying... but no, I don't understand :)
[/quote]

function insertrow($order_num, $item_name, $item_price, $item_qty,$item,$inv_num)
{
list($order_num, $item_name, $item_price, $item_qty) =
split("~", $item);

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

above code is just a function which inserts rows:
function name: insertrow();
input parameters: $ordernum, $itemname, $itemprice, $itemqty,$item,$invnum

$item = current item to be inserted.

let you have three items, which are $item1,$item2,$item3
now,
$item[1] = $item1;
$item[2] = $item2;
$item[3] = $item3;
$order_num[1][2][3] = $order_num1, $order_num2,$order_num3
$itemname.........
$itemprice.................
$itemqty.......................
$invnum............................

$number_of_items = 3; //total number of items to be inserted.
now just call that function
for($K=1;$K<=$number_of_items;$K++)
{
insertrow($ordernum[$K], $itemname[$K], $itemprice[$K], $itemqty[$K],$item[$K],$invnum[$K]);
}

enough? :)

www.karthi.us

#5 Chet

Chet
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 April 2006 - 08:04 PM

Guess I'm not ready for this php stuff yet..
Thanks for taking the time to try to help out.

#6 karthikeyan_coder

karthikeyan_coder
  • Members
  • PipPipPip
  • Advanced Member
  • 201 posts

Posted 10 April 2006 - 08:31 PM

ok np. :)
www.karthi.us




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users