PapaBurgundy Posted May 7, 2011 Share Posted May 7, 2011 Basically I'm trying to set up a shopping cart using PHP & MySql (oh really!?) and I've gotten to the point where I need to insert the bought products into a database. Currently, at the checkout, there is a session which stores data for all of the products which have been added into the cart. From here I am just trying to create an order id code that is only relevant to this session. So in the database it would end up looking something like this: order_id = 001, product_id = 2, order_quantity = 3 order_id = 001, product_id = 4, order_quantity = 2 order_id = 001, product_id = 1, order_quantity = 5 order_id = 002, product_id = 2, order_quantity = 3 order_id = 002, product_id = 4, order_quantity = 2 order_id = 002, product_id = 1, order_quantity = 5 So I would be able to pull these results later by selecting the order relevant to the order_id. At least I think this is the easiest option for me. Every product which is put into the cart has a $_SESSION name of 'product_x', 'x' being the id associated with that product Any help? Quote Link to comment Share on other sites More sharing options...
fugix Posted May 7, 2011 Share Posted May 7, 2011 everytime an order is placed you could set a $_SESSION[] or var = to a value that auto increments...eg $i=1; if(order_started) { $_SESSION['order'] = $i; $i++; } something like that Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 7, 2011 Share Posted May 7, 2011 When someone finalizes an order, you would insert a row into an `order` table. That table would have the order_id as an auto-increment column. It would also have columns for the user_id, order date, order status, shipping information... You would get the auto-increment value from the above table using a function like mysql_insert_id and use that value when you insert the details of the order into your order_id, product_id, order_quantity table. Quote Link to comment Share on other sites More sharing options...
spiderwell Posted May 7, 2011 Share Posted May 7, 2011 why not use the sessionid? surely that would be the obvious unique identifier for each session order id code Quote Link to comment Share on other sites More sharing options...
Zane Posted May 8, 2011 Share Posted May 8, 2011 Having an auto-increment field only increment upon a condition completely takes the auto out of the incrementation. If it is possible then it's most likely a poor design. Your best bet is to add another field called session and keep the id field as a primary key that auto increments. Doing this will give you the ability to join to the users table and collect that users data along with the 'ordered' products. Quote Link to comment Share on other sites More sharing options...
PapaBurgundy Posted May 8, 2011 Author Share Posted May 8, 2011 The problem with using session id is that there is a session id for each product in the cart. So if I were to put this into the database it would store each product individually, which I want, but it wouldn't have an id code for the entire session. Also, I'm guessing that if I auto incremented an id for the order, it would auto increment with every product entered into the database on that session. So products from the same order would have different order ids. I'm gonna try the mysql_insert_id() and see what I can come up with. Otherwise I'm gonna try some dodgy approach of entering the current time and date for each order and then referencing those to a separate id. Thanks! Quote Link to comment Share on other sites More sharing options...
spiderwell Posted May 8, 2011 Share Posted May 8, 2011 The problem with using session id is that there is a session id for each product in the cart. session id is unique per visitor and consistent for that visitor until the session expires. I don't see how you would get a seperate sessionid per product Quote Link to comment Share on other sites More sharing options...
PapaBurgundy Posted May 8, 2011 Author Share Posted May 8, 2011 Hmmm maybe I'm thinking about this too hard then? EDIT** Half way through typing this I finally understand what you're all on about, and yeah I was thinking too hard haha. I used what fugix had posted and it works. So simple Thanks! Quote Link to comment Share on other sites More sharing options...
PapaBurgundy Posted May 8, 2011 Author Share Posted May 8, 2011 Hmmm maybe I'm thinking about this too hard then? EDIT** Half way through typing this I finally understand what you're all on about, and yeah I was thinking too hard haha. I used what fugix had posted and it works. So simple Thanks! I thought I had it sussed, but after a few more hours of attempting to get it working I am again stuck. I started using the session_id() which brought up nice long session id. From this I can see how it can be used, however is there a way to get this session_id() is auto increment or change after the person's details have been submitted? I've tried all sorts, I think the closest I got was $session_id = session_id(); $session_id++; Unfortunately this only incremented once, I tried placing it after the order details had gone through as well but still nothing. I also thought about a function which checks to see if the session_id(), is equal to or less than, session_id in the database. This way the next session_id() would always be different Quote Link to comment Share on other sites More sharing options...
spiderwell Posted May 8, 2011 Share Posted May 8, 2011 I think if you really want auto increment the only real other option is an invoice table on the db which will have the obvious Id column that auto increments, and perhaps a second column that is userid. that way you can when a new order is placed is create a row, and take the new ID and pass that as the orderid to the table you already have. it will mean a bit more coding but it should make it all much more manageable in the long run Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 8, 2011 Share Posted May 8, 2011 You are making this about four times harder than it is - When someone finalizes an order, you would insert a row into an `order` table. That table would have the order_id as an auto-increment column. It would also have columns for the user_id, order date, order status, shipping information... You would get the auto-increment value from the above table using a function like mysql_insert_id and use that value when you insert the details of the order into your order_id, product_id, order_quantity table. 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.