MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 ok, well I don't know if your current script is going to particularly work for that sought of thing :-\ I think maybe make a new page, and set it up like the following: <?php // session variables into common variables $cart = $_SESSION["cart"]; $item = $_SESSION["item"]; // or whatever your session variables are // then run a INSERT INTO query for all of them into the database with all the $rs variables of course $sql = "INSERT INTO orders (cart,item) VALUES ('$cart','$item')"; // then once you have done all the inserts have it redirect to whatever page you want header("Location: page.php"); I think the fact that you have an Array() inside a session variable is whats stuffing things up, I've never worked with Arrays inside session variables so I dont know how to grab the data from inside the array when its in a session variable, but I think the way to get this to work is to select each separate bit of data from inside the array and then insert it into the database and this will fix the problem. I made a function to do a similar thing like this before, so maybe you might be able to modify it to select data from your array inside your session variable: <?php // Select 1 field from an Array function select_array($array,$id,$field) { return $array[$id][$field]; // the format is Array, ID of array your selecting within the main array, the field you // select from inside the array. // you need to somehow change it to grab the array from the session var and then // select all the fields within the array and then put them into common variables to // insert into the database } Regards ACE Link to comment https://forums.phpfreaks.com/topic/72127-save-session-shopping-cart-data-into-database/page/2/#findComment-363740 Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 before i change that is this any good??? http://www.phpbuilder.com/board/showthread.php?p=10827423#post10827423 Link to comment https://forums.phpfreaks.com/topic/72127-save-session-shopping-cart-data-into-database/page/2/#findComment-363746 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 not particularly lol, Its just a dead end I'm gonna have a read through my PHP textbooks and see if I can find anything on arrays in session variables. EDIT: just got a idea, why not try a foreach statement? lets try a few different things with a foreach statement and see what we come up with. <?php session_start(); $common_variable = $_SESSION['cart']; foreach($common_variable as $key => $val) { echo $key; echo "<hr>"; echo $val; } give that a try and see if it displays anything. Link to comment https://forums.phpfreaks.com/topic/72127-save-session-shopping-cart-data-into-database/page/2/#findComment-363747 Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 tried the new way and still get the word array! ??? Link to comment https://forums.phpfreaks.com/topic/72127-save-session-shopping-cart-data-into-database/page/2/#findComment-363750 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 hmm ok, back to the drawing board lol <?php session_start(); $common_variable = $_SESSION['cart']; foreach($common_variable as $key => $val) { echo $key; echo "<hr>"; echo $val; echo "<hr>"; echo count($common_variable); echo "<br>"; echo count($key); echo "<br>"; echo count($val); } see what that says. Link to comment https://forums.phpfreaks.com/topic/72127-save-session-shopping-cart-data-into-database/page/2/#findComment-363751 Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 Great --- this works but i think a little work is needed. <?php session_start(); $rs = mysql_connect( ); $rs = mysql_select_db( ); $cart = $_SESSION['cart']; foreach($cart as $items); $sql = "INSERT INTO orders (items) VALUES ('$items')"; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); header("location:pay.php"); ?> but i only get the id of the last product added to the cart! Link to comment https://forums.phpfreaks.com/topic/72127-save-session-shopping-cart-data-into-database/page/2/#findComment-363753 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 try something like this after the foreach statement: <?php $item_id = $items['id']; and you can do that for each of the fields in the array, and then insert each common variable, eg $item_id into the database. Link to comment https://forums.phpfreaks.com/topic/72127-save-session-shopping-cart-data-into-database/page/2/#findComment-363755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.