axiom007 Posted September 28, 2007 Share Posted September 28, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/ Share on other sites More sharing options...
pocobueno1388 Posted September 28, 2007 Share Posted September 28, 2007 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()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357165 Share on other sites More sharing options...
darkfreaks Posted September 28, 2007 Share Posted September 28, 2007 would it be better to array $products= $_POST['item']; Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357166 Share on other sites More sharing options...
axiom007 Posted September 28, 2007 Author Share Posted September 28, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357169 Share on other sites More sharing options...
darkfreaks Posted September 28, 2007 Share Posted September 28, 2007 well some code would go along way we dont know how your cart is setup to display items Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357171 Share on other sites More sharing options...
axiom007 Posted September 28, 2007 Author Share Posted September 28, 2007 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'] Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357177 Share on other sites More sharing options...
pocobueno1388 Posted September 28, 2007 Share Posted September 28, 2007 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()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357204 Share on other sites More sharing options...
axiom007 Posted September 28, 2007 Author Share Posted September 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357210 Share on other sites More sharing options...
pocobueno1388 Posted September 28, 2007 Share Posted September 28, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357304 Share on other sites More sharing options...
axiom007 Posted September 28, 2007 Author Share Posted September 28, 2007 thank for your help so far. I used the loop that you suggested and it inserted all of the information properly, however, it only inserted on record when there should have been two. Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357544 Share on other sites More sharing options...
pocobueno1388 Posted September 28, 2007 Share Posted September 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357545 Share on other sites More sharing options...
axiom007 Posted September 28, 2007 Author Share Posted September 28, 2007 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 = Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357553 Share on other sites More sharing options...
pocobueno1388 Posted September 28, 2007 Share Posted September 28, 2007 Hmm...I'm not sure what to tell you. I don't see a reason why the while loop would only go through one of the records if there are two. Could you post your updated code? I doubt that will show the answer, but I don't know where else to look. Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357556 Share on other sites More sharing options...
axiom007 Posted September 28, 2007 Author Share Posted September 28, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357568 Share on other sites More sharing options...
pocobueno1388 Posted September 28, 2007 Share Posted September 28, 2007 Hah, I see the problem. You need to take this line out: $row_addorder = mysql_fetch_assoc($addorder); It should work fine after that. Quote Link to comment https://forums.phpfreaks.com/topic/71036-shopping-cart-help/#findComment-357571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.