Hi DKY,
I do this so that every time someone places an order the fields are populated
Imagine you have 1000 products.. are you going to create 1000 extra fields?? 😕
Like I said before if you do this then the whole point of a relational database is lost.
Lets get the objectives of yours first.
- System can have any no of products.
- Users can purchase any product with any quantity
- user should be able to re-order the same order later on.
so solving part 🙂
- System can have any no of products.
you can simply have a table called products with following fields.
products
id
name
so each time you add a new product a new record will be added to the table. REMEMBER ITS A RECORD NOT A FIELD. make sure you specify the id field as a auto number so when ever you add a product the table will generate an auto number for that product.
- Users can purchase any product with any quantity
To simplify the process i recommend to have 2 tables as,
orders
id --------- auto number (primary key)
userId
orderDate
orderDetails
id --------- auto number (primary key)
orderId ----- (foreign key)
productId ----- (foreign key)
qty
unitPrice *
you can remove the unitPrice since you don`t want to store the prices. so when ever user place an order,
a) insert userId, date to orders table.
b) get the last insert id from orders table.
c) insert the product details one by one to the orderDetails table. make sure to use last insert id as orderId.
so now we have all the order details.
- user should be able to re-order the same order later on.
This is simple. Since we have order details stored in our table for that perticular user all you need to do is search for all the orders place by that user in orders table using userID and get the details of that order from orderDetails table using orderId.
Hope this is clear for you.
Thanks,
best regards,
Niroshan