derm116 Posted March 27, 2007 Share Posted March 27, 2007 Hi all, Just wondering if anyone could please help me?? I'm writing a basic shopping cart function in php. I've created a databse in mysql containing a user, a cart and a book among other things. I've used the following function to create a cookie function GetCartID() { /* This function will generate an encrypted string and will set it as a cookie using set_cookie. This will also be used as the cookieID field in the cart table. */ if(isset($_COOKIE["cartID"])) { return $_COOKIE["cartID"]; } else { /* There is no cookie set. We will set the cookie and return the value of the users session ID */ session_start(); setcookie("cartID", session_id(), time() + ((3600 * 24) * 30)); return session_id(); } } In my cart page then i set the users username and password to the cookie as follows $username = $_COOKIE["user_derm116"]; $password = $_COOKIE["pw_derm116"]; with derm116 been the name of my database My problem is when i am trying to add items to the cart, done as follows if($numRows == 0) { /* This item doesn't exist in the users cart, we will add it with an insert query */ $query = "INSERT INTO cart (cookieID, bookID, qty) VALUES ('" . GetCartID() . "', $bookID, $qty)"; @mysql_query($query) or die("Query failed"); } The problem is, the cookie becomes a string when it is created but my userid is a auto-incrementing int, which means i can't connect the 2!!! I've tried loads of different ways to get over this problem but can't figure it out and its driving me mad!!! I need to connect the user to the cart so that everytime they add an item to the cart it's saved to their cart. Does anyone have any idea on how i could connect the user to the cart??? Thanks a mil in advance Regards, Derm Quote Link to comment Share on other sites More sharing options...
per1os Posted March 27, 2007 Share Posted March 27, 2007 If I am understanding it correctly you simply want to convert the cookie userid to an int? http://us2.php.net/manual/en/function.settype.php That should do what you need it to. If not than this is just very confusing on what you are trying to do and what is the end result that you want. Quote Link to comment Share on other sites More sharing options...
derm116 Posted March 27, 2007 Author Share Posted March 27, 2007 Sorry, i'm even starting to confuse myself now. What i want to do is to link my user table and my cart tables somehow. Each session creates a unique cookie using the sessionId. I want to somehow have the cookie and user linked to the cart so that each time a user adds an item to the cart, the cart is changed to show the new item added to the cart. For the remainder of the session the user should be able to check their cart and see all the items they've added so far. To do this both the cookie and the user need to be linked to the cart??? Any ideas? Hope thats a bit clearer, tanx Quote Link to comment Share on other sites More sharing options...
per1os Posted March 27, 2007 Share Posted March 27, 2007 Databases should be in 3rd normal form, which in essence is what you are trying to get it. I would suggest having an Auto_increment catid for the category table and than put the catid in the table that you want to link to. Search for 3NF under SQL it should explain it some more. Quote Link to comment Share on other sites More sharing options...
derm116 Posted March 27, 2007 Author Share Posted March 27, 2007 I think my tables are in 3NF already For the 3tables been used here, book has a primary key bookid - int - auto-increment cart has a primary key cartid - int - auto-increment user has a primary key userid - int - auto-increment Originally i had a foreign key called userid in my cart table which linked back to userid, and a foreign key in the cart table called bookid linking back to book, however on creating the cookie the userid becomes a string which can't link back to the int in the user table!! Just can't figure it out! any ideas on what i might be doing wrong?? Tanx a mil Quote Link to comment Share on other sites More sharing options...
per1os Posted March 27, 2007 Share Posted March 27, 2007 What you say does not really make sense. I understand that creating a cookie makes it "a string" but using the settype should fix that problem just call that settype on the cookie and it should convert it to int. than make your DB calls for that userid. 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.