Jump to content

Creating an auto incrementing id which only does it once per session?


PapaBurgundy

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.