Jump to content

shopping basket duplicate entries


chriso20

Recommended Posts

Hey folks!

 

I'm working on a shopping basket.

 

I have a table called 'baskets' where i store the member id, product id and quantity of products. So when a member adds something to their basket, they're adding it to this table.

 

 

I cracked the "well what if someone adds an item that's already in their basket?" problem with this code:

 

INSERT INTO baskets(memberId, productId, quantity)
VALUES 
('".$_SESSION['memberId']."', '".$_POST['productId']."', '".$_POST['qty']."')
on duplicate key update quantity = quantity+".$_POST['qty'];

(ignore the possible SQL insertion)

 

The problem is that this only checks the primary key - the member's id. Not the product id as well.

 

So adding 50 of product "A", then adding 100 of product "B" simply updates the database to be 150 of product "A", not adding a new entry for the new product.

 

 

 

Any help with this would be really appreciated!

 

Chris

Link to comment
Share on other sites

i can't make the productId unique, otherwise only 1 member could have that product in their basket at a time.

 

Multiple rows would be a pain in the arse, you could potentially have 100 rows for the same user buying the same item... not very efficient!

 

Any other ideas?

 

Or should i just do a quick SQL query for that member and product and if it exists, increase the quantity - if not create a new row?

Link to comment
Share on other sites

Ok i've got it functioning as expected but the SQL i've used is not as efficient as it should be;

 

I query the table for that member's id and the product id.

 

If the returned rows is 0, i insert the data, if it's not 0, i update the data.

 

In this same script i'm also going to have to do a multi-table query to find the prices of the products and produce an updated grand total for the products in the basket.

 

 

Is this script a little too database intensive or am i just worrying for no good reason?

Link to comment
Share on other sites

i can't make the productId unique, otherwise only 1 member could have that product in their basket at a time.

You're not reading what I wrote -- a multi-column index.

 

Multiple rows would be a pain in the arse, you could potentially have 100 rows for the same user buying the same item... not very efficient!

Yes, but you won't have 100 all of the time, and then you can keep track of when this happens.

 

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.