benh5851 Posted October 6, 2007 Share Posted October 6, 2007 hi , i have a running shopping cart and i need to save the data in the cart to a orders database. the items in the cart are stored in a session by their id and then are displayed in the cart by sending a query to the items database finding their name, price and product code. how can i put the carts data into a db called orders. the items are stored into the session by... PHP Code: <? session_start(); if ( !isset( $_SESSION["cart"] ) ) { $_SESSION["cart"] = array(); } $id = $_POST["id"]; $_SESSION["cart"][$id] = $id; header("location:index.php"); ?> thanks a lot ben Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted October 6, 2007 Share Posted October 6, 2007 I asume you build the part where a costomer has to register and log in too. You just make the user log in on his account then let him/her proceed on the shopping cart. once the user wants to check out you just insert a record into a order table. Things that are unclear are: if you have experience with sql what kind of sql your using if you have the user log on system tell these things and you will get better help Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 6, 2007 Author Share Posted October 6, 2007 hi, yes i allready have the login script and i know the ususal sql code.. see here http://tom.btronics.co.uk thanks Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 then all your looking for is something like this: <?php // make common variables for the session variables $id = $_POST["id"]; $_SESSION["cart"][$id] = $id; $common_variable = $_SESSION["cart"][$id]; $rs = mysql_connect( "localhost", "username", "password" ); $rs = mysql_select_db( "database" ); // SQL query $sql = "INSERT INTO `table` " . $common_variable . " WHERE id=$user_id"; echo $sql; // echo it for debugging purpose's and to make sure everything is ok. $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); or somthing along those lines, MySQL queries are exactly the same when you use session variables, you can just put the session variables into common variables and then INSERT into the database. Regards ACE Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 ok thanks.... i have modified it to suit, the id i dont think i need as the purpose of the script is to save the shopping cart into a db so the order can be completed. <? session_start(); $common_variable = $_SESSION["cart"]; $rs = mysql_connect( "xxx", "xxx", "xxx" ); $rs = mysql_select_db( "xxx" ); // SQL query $sql = "INSERT INTO `orders` " . $common_variable . " WHERE order_id=1"; echo $sql; // echo it for debugging purpose's and to make sure everything is ok. $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); header("location:pay.php"); mysql_close($link ); ?> though i get INSERT INTO `orders` WHERE order_id=1Query: INSERT INTO `orders` WHERE order_id=1 Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE order_id=1' at line 1 Thanks Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 try this: <?php $sql = "INSERT INTO `orders` " . $common_variable . " WHERE order_id='1'"; Note I added the single comma's to the 1 Regards ACE Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 now this Parse error: syntax error, unexpected ':' in /mounted-storage/home43c/sub002/sc32535-PERI/v2/cart/order.php on line 10 Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 could you post line 10? or is this line 10? $sql = "INSERT INTO `orders` " . $common_variable . " WHERE order_id=1"; Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 sorry my fault had : from where i had copied and pasted it - stupid me ! but i still get this error... INSERT INTO orders WHERE order_id='1' Query: INSERT INTO orders WHERE order_id='1' Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE order_id='1'' at line 1 This is the code <? session_start(); $common_variable = $_SESSION["cart"]; $rs = mysql_connect( "xxx", "xxxx", "xxxx" ); $rs = mysql_select_db( "btronics" ); $sql = "INSERT INTO orders " . $common_variable . " WHERE order_id='1' "; echo $sql; // echo it for debugging purpose's and to make sure everything is ok. $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); header("location:pay.php"); mysql_close($link ); ?> Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 try this: INSERT INTO `orders` WHERE `order_id`='1' and change your MySQL info into a asterix's(*) EDIT: I know what we've done wrong here. We've got INSERT INTO, then we go to WHERE, we are missing a part here lol. <?php $sql = "INSERT INTO `orders` (colum_your_inserting_into) VALUES ('$common_variable') WHERE order_id='1'; that should work then. Regards ACE Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 Done That! still no look though Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 just copy and paste the $sql variable over your current one, and for the first pair of brackets, put in the name of the column you are inserting into with single comma's on either side 'column_name' so if your column is called 'order_number' you would do this: <?php $sql = "INSERT INTO `orders` ('order_number') VALUES ('$common_variable') WHERE order_id='1'; I'll break it down for you. INSERT INTO 'what table you are editing', ('column') 'what column of the table you are editing' VALUES () 'what is actually being inserted into that column' WHERE 'who's account or particular entry in the table you are editing' EDIT: is their any entries in this table in the moment? Regards ACE Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 hi yes i understud that i messed the time up as i edited the db login infov lol! is it possible you could give me the complete script i think i have a error somewhere the table is orders and the colum is items thanks for your help m8 Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 SORTED -nearly... the reason an error was displayed was because i had created an id of 1 before so if i wanted to write over the top i would need update i think. 1 problem though i just get an empty row in my db?? thanks ben Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 <?php session_start(); $common_variable = $_SESSION["cart"]; $rs = mysql_connect( "xxx", "xxxx", "xxxx" ); $rs = mysql_select_db( "btronics" ); $sql = "INSERT INTO `orders` ('items') VALUES ('$common_variable') WHERE order_id='1'"; echo $sql; // echo it for debugging purpose's and to make sure everything is ok. $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // header("location:pay.php"); // commented out for now while we debug mysql_close($rs ); // were closing the wrong connection, $link doesn't exist lol ?> give that a go, leave the header function commented out for now. And tell me what is echo'd from $sql. EDIT: I missed out a double comma sorry, copy and paste this code again. Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 INSERT INTO `orders` ('items') VALUES ('Array') WHERE order_id='1'Query: INSERT INTO `orders` ('items') VALUES ('Array') WHERE order_id='1' Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''items') VALUES ('Array') WHERE order_id='1'' at line 1 Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 we dont need where because i need it to creat a new one! i now get INSERT INTO `orders` ('items') VALUES ('Array')Query: INSERT INTO `orders` ('items') VALUES ('Array') Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''items') VALUES ('Array')' at line 1 Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 whats this Array value? lol Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 i thinks its how the id is saved when an item is added to the carts session??? this is my total code now add to cart <? session_start(); if ( !isset( $_SESSION["cart"] ) ) { $_SESSION["cart"] = array(); } $id = $_POST["id"]; $_SESSION["cart"][$id] = $id; header("location:index.php"); ?> then the order bit <?php session_start(); $common_variable = $_SESSION["cart"]; $rs = mysql_connect(xxx); $rs = mysql_select_db(xxx ); $sql = "INSERT INTO orders ('items') VALUES ('$common_variable')"; echo $sql; // echo it for debugging purpose's and to make sure everything is ok. $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // header("location:pay.php"); // commented out for now while we debug mysql_close($rs ); // were closing the wrong connection, $link doesn't exist lol ?> Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 the id is from a hidden field on the form of the add to cart button. if this helps Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 done a bit of editing now get this INSERT INTO orders (items) VALUES ('Array') <?php session_start(); $common_variable = $_SESSION['cart']; $rs = mysql_connect( xxx); $rs = mysql_select_db(xxx); $sql = "INSERT INTO orders (items) VALUES ('$common_variable')"; echo $sql; // echo it for debugging purpose's and to make sure everything is ok. $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // header("location:pay.php"); // commented out for now while we debug ?> Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 ok, lets try a couple tests. on the order page. type this after $common_variable = $_SESSION["cart"]; <?php // leave out the <?php of course, just for displaying code correctly on the forum ^^ echo $common_variable; echo "<br>"; then comment out all $rs variables on the page. And comment out mysql_close at the bottom their. and then hopefully, it will display what is in the session variable and what is in $sql, if both those variables are holding the correct values, we'll move onto the next step. Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 Array INSERT INTO orders (items) VALUES ('Array') this is displayes, there is no connection problem because in my mysql controlpanel their are entries with the word array in the items colum! this is where i am testing the code http://tom.btronics.co.uk/cart/order.php Thanks Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 hmmm, this one has me stumped lol ok, uncomment everything, except for the header() function. what exactly is it suppose to put into the database? I know $_SESSION["cart"] is going into the database, but what is your expected result? Quote Link to comment Share on other sites More sharing options...
benh5851 Posted October 7, 2007 Author Share Posted October 7, 2007 ok, i want the carts sessions data to be stored (items ids) so i can build like a order backend application. i had this problem when i tried to get the session data into the cart before but i cant remember how i got round it! thanks Quote Link to comment 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.