johnnystarfish — 2012-11-28T18:28:59-05:00 — #1
I'm trying to put together a pseudo e-commerce solution for a school project. I think I have the schema down but I'm having a bit of trouble figuring out how to actually insert orders into the db.
Here is a diagram of my schema.
The "status" table is a lookup table for indicating the current state of an order, denoted by a numeric code. 1 - RECEIVED, 2 - CANCELLED, 6 - SHIPPED, etc.
id and orderno are both auto-incremented fields.
At this moment I am stuck trying to construct a query or sequence of such that would actually populate the database with a single order.
I made a query to enter customer info. That much works; he gets assigned an id number.
insert into customers set firstname.lastname@example.org', name='Ernie Bert';
Now I assume I need to make a new entry in the orderinfo table to establish a new orderno for this customer, so I can tie individual line items to it. How do I do this without having to personally look up customer.id and insert it in the query? Also how can I make sure the orderinfo table is treating the id field as customer.id? I used a series of alter table commands to establish foreign keys but I don't see any indication anywhere that it actually worked.
r937 — 2012-11-28T18:48:32-05:00 — #2
when you add the customer, use mysql's LAST_INSERT_ID() function to grab the value of the customer id
(i'm assuming mysql because you mentioned "auto-incremented" but there are similar functions in other database systems)
then use that retrieved value for the id column when you create the orderinfo row (it would probably be better named as customerid)
by the way, your schema is fine
johnnystarfish — 2012-11-28T19:03:21-05:00 — #3
Thanks for responding, but I'm not quite sure where to go with it. From what I am seeing about that function, it only works when called within the same connection, otherwise it returns 0. Right now I'm trying to type in an order by hand through the mysql interface just to make sure I've got the query syntax correct, and trying to call that function is just returning 0 as expected.
r937 — 2012-11-28T19:58:42-05:00 — #4
what language are you going to use to run your e-commerce app? php?
you'll want to use php's mysql_insert() function instead
as far as testing out your sql manually, that's fine, you can replace the SELECT for LAST_INSERT_ID with a simple SELECT to get the id based on a WHERE clause using the same values that you added the customer with