문제

I have a shopping cart that stores itemIds and quantities per item in Sessions until we get to checkout.

After the client pays, I proceed to insert the information in my ORDERS table in a database.

My question is: How should I handle one order from a client that has multiple items? Multiple quantities is simple since my ORDERS table has a 'quantity' field. But how about multiple items? What is recommended? Should I INSERT different lines per item for one same order?

Currently I was having this:

$query = "INSERT INTO orders (itemId, quantity, clientPrice, firstName, lastName, email, phoneNumber, address, city, zip) 
VALUES (:itemId, :quantity, :clientPrice, :firstName, :lastName, :email, :phoneNumber, :address, :city, :zip)";
도움이 되었습니까?

해결책

In my opinion you should create 2 tables with at least such fields:

orders

id | user_id | date

ordered_items - you must have full table of items as well, besides this table, containing all the info about your products. Price here is needed just because prices of your items can change and you must not change 'amount of money spent' on already created orders.

item_id | order_id | quantity | price

So you could then use joins and count everything you need - table joins will do all the work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top