Jump to content

Shopping cart help


axiom007

Recommended Posts

Hello all,

A newbie here.  I am helping some really cheap friends develop an ecommerce site.  I pretty much have everything in order except for one small thing.  When the customer ultimately checks out, I need to add all of the item to the order items table.  This could be one row, it could be twenty rows, ya know, however many different things they have selected.  How can I make it insert each record insert into the order items table on one click.  I apologize for the naivity of the question, but any help would be appreciated.

 

Thanks

-Richard

Link to comment
Share on other sites

You need to use a loop. I'm assuming the items are stored in an array? Then just do something like this:

 

<?php

$items = array("item1", "item2", "item3");

foreach ($items as $item){
   $query = "INSERT INTO orders (item) VALUES ('$item')";
   $result = mysql_query($query)or die(mysql_error());
}

?>

Link to comment
Share on other sites

as of right now, the cart has it's own table that is pulls the correct rows per that session.  I do apologize fro my extreme ignorance, but I am not terribly familiar with using arrays, but am always wanting to learn more.  Would storing the values into an array be a better way to go?  Also, in looking at your example, you show (item1),(item2),(item3).  in defininf the array, do you have to have a set number of items? in the cart there could be one item, or a whole bunch of them, i.e. more than 3. once again, sorry for the ignorance, just trying to learn.

 

Thanks

-Richard

Link to comment
Share on other sites

mysql_select_db($database_ccc, $ccc);

$query_viewcart = sprintf("SELECT shop_tracker.shop_id, shop_tracker.card_qty, shop_tracker.item_total, card_items.card_name, card_items.card_sku, card_items.image, shop_tracker.card_id FROM shop_tracker LEFT JOIN card_items on shop_tracker.card_id = card_items.card_id WHERE session_id = '".$_COOKIE['PHPSESSID']."'")

$viewcart = mysql_query($query_viewcart, $ccc) or die(mysql_error());

$row_viewcart = mysql_fetch_assoc($viewcart);

$totalRows_viewcart = mysql_num_rows($viewcart);

 

This is my query to pull the coreect information. the only other thing I am doing with it at this point is displaying the values:

 

$row_viewcart['column_name']

Link to comment
Share on other sites

So your basically just trying to transfer the items from the cart table in the DB to the order table of the db when they checkout?

 

Then you will have to use a while loop.

 

<?php

while ($row_viewcart = mysql_fetch_assoc($viewcart)){
   $query = "INSERT INTO orders (col1, col2) VALUES ('{$row_viewcart['val1']}', '{$row_viewcart['val1']}'":
   $result = mysql_query($query)or die(mysql_error());
}

?>

Link to comment
Share on other sites

So your basically just trying to transfer the items from the cart table in the DB to the order table of the db when they checkout?

 

Then you will have to use a while loop.

 

<?php

while ($row_viewcart = mysql_fetch_assoc($viewcart)){
   $query = "INSERT INTO orders (col1, col2) VALUES ('{$row_viewcart['val1']}', '{$row_viewcart['val1']}'":
   $result = mysql_query($query)or die(mysql_error());
}

?>

 

Thank you so much. One last thing, then I will stop bothering you.

 

I am assuming Col1, col2 are my column names, but for values there are 2, do I need a {$row_viewcart['val1']} for each row? because I will not know how many rows are going to be inserted.

 

Thank you again.

Link to comment
Share on other sites

You don't need to know how many rows will be inserted, the loop will take care of that for you no matter how many rows there are.

 

Yes, if there are two columns, your going to need two variables from the database. I can't really help you much with that part as I don't know what your transferring over to the orders table...it shouldn't be hard to figure out though if you know what you want...

 

 

Link to comment
Share on other sites

Go into phpmyadmin and put in this query:

 

SELECT shop_tracker.shop_id, shop_tracker.card_qty, shop_tracker.item_total, card_items.card_name, card_items.card_sku, card_items.image, shop_tracker.card_id FROM shop_tracker LEFT JOIN card_items on shop_tracker.card_id = card_items.card_id WHERE session_id = '".$_COOKIE['PHPSESSID']."'

 

Obviously your going to need to replace the cookie variable with something.

 

I bet it only returns one result and you are just thinking it should return two. Double check it.

Link to comment
Share on other sites

ok, I see why that would only return one because my shop_id is unique. I took that into consideration when I wrote the query for the multiple records, and this is what I used that only inserted one result but when run through phpMyAdmin returned two results.

 

SELECT shop_tracker.shop_id, shop_tracker.card_qty, shop_tracker.item_total, card_items.card_name, card_items.card_sku, card_items.image, shop_tracker.card_id, shop_tracker.card_id, shop_tracker.card_qty, shop_tracker.name_pers_1, shop_tracker.name_pers_2, shop_tracker.name_pers_3, shop_tracker.name_pers_4, shop_tracker.vers_pers_1, shop_tracker.vers_pers_2, shop_tracker.vers_pers_3, shop_tracker.vers_pers_4, shop_tracker.vers_pers_5, shop_tracker.vers_pers_6, shop_tracker.vers_pers_7, shop_tracker.env_pers_1, shop_tracker.env_pers_2, shop_tracker.env_pers_3, shop_tracker.item_total, shop_tracker.custom_verse, shop_tracker.custom_env
FROM shop_tracker
LEFT JOIN card_items ON shop_tracker.card_id = card_items.card_id
WHERE session_id =

Link to comment
Share on other sites

mysql_select_db($database_ccc, $ccc);
$query_addorder = sprintf("SELECT shop_tracker.card_qty, shop_tracker.item_total, card_items.card_name, card_items.card_sku, card_items.image, shop_tracker.card_id, shop_tracker.card_qty, shop_tracker.name_pers_1, shop_tracker.name_pers_2, shop_tracker.name_pers_3, shop_tracker.name_pers_4, shop_tracker.vers_pers_1, shop_tracker.vers_pers_2, shop_tracker.vers_pers_3, shop_tracker.vers_pers_4, shop_tracker.vers_pers_5, shop_tracker.vers_pers_6, shop_tracker.vers_pers_7, shop_tracker.env_pers_1, shop_tracker.env_pers_2, shop_tracker.env_pers_3, shop_tracker.item_total, shop_tracker.custom_verse, shop_tracker.custom_env FROM shop_tracker LEFT JOIN card_items on shop_tracker.card_id = card_items.card_id WHERE session_id = '".$_COOKIE['PHPSESSID']."'");
$addorder = mysql_query($query_addorder, $ccc) or die(mysql_error());
$row_addorder = mysql_fetch_assoc($addorder);
$totalRows_addorder = mysql_num_rows($addorder);

while ($row_addorder = mysql_fetch_assoc($addorder)){
$query = sprintf("INSERT INTO card_order_items (card_name, card_qty, card_sku, env_pers_1, env_pers_2, env_pers_3, name_pers_1, name_pers_2, name_pers_3, name_pers_4, order_id, session_id, total_price, verse_pers_1, verse_pers_2, verse_pers_3, verse_pers_4, verse_pers_5, verse_pers_6, verse_pers_7) VALUES ('{$row_addorder['card_name']}', '{$row_addorder['card_qty']}', '{$row_addorder['card_sku']}', '{$row_addorder['env_pers_1']}', '{$row_addorder['env_pers_2']}', '{$row_addorder['env_pers_3']}', '{$row_addorder['name_pers_1']}', '{$row_rs_addorder['name_pers_2']}', '{$row_rs_addorder['name_pers_3']}', '{$row_rs_addorder['name_pers_4']}', '{$_SESSION['order_id']}', '{$sid}', '{$row_addorder['item_total']}', '{$row_addorder['vers_pers_1']}', '{$row_addorder['vers_pers_2']}', '{$row_addorder['vers_pers_3']}', '{$row_addorder['vers_pers_4']}', '{$row_addorder['vers_pers_5']}', '{$row_addorder['vers_pers_6']}', '{$row_addorder['vers_pers_7']}')");
$result = mysql_query($query)or die(mysql_error());

 

Maybe you can see something that I am missing.

 

Thank you

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.