For your first question, this depends on whether you calculate the costs per line or per order. Whichever one works for you.
The second question:
Database tables are not really ‘linked’ until you query them - granted you can put contraints on to stop lines being put in the lines table without a corresponding header, but still this is not really ‘linking’ the tables, it simply ensures data integrity.
You may want to use stored procedures for manipulating the data - if you don’t know what these are:
They are basically pre-compiled queries which more often than not contain a bunch of parameters that you pass in. Often they are a bit quicker than regular queries because of the pre-compiled nature (the SQL server does not need to figure out the execution plan for the query as it’s already worked one out)
You call them via the exec statement and then pass the parameters accordingly - I’m not 100% on the MySQL syntax as I’m more a microsoft SQL user but the details should be in the MySQL docs.
The advantage over a stored procedure is that once you’ve written it you’ve only got one place to maintain your SQL and you can run it from any place in your application which means that you don’t need to write an INSERT statement directly in your app and you won’t find yourself creating bugs in a piece of logic which is supposed to perform the same task as another piece.
Saying that however, most well written applications should seperate data, business and presentation into 3 distinct layers. Basically you would be writing some classes or modules for data access, some classes which will be your business objects and finally classes which will render the business data to the screen.
So your simple model will look like:
Data -> Business Layer -> Presentation Layer
To actually ‘link’ your data your SQL statements/stored procedures need to associate fields in each table with each other.
There are many ways to get data and the T-SQL language can be very frustrating if you aren’t exactly sure what you are doing with it.
You might want to read up on SQL - in your case you probably won’t need to write and difficult complex queries, a simple select and join should do
You will just need to select the data you want from the tables you want and join the linking fields together
SELECT
<distinct fieldname from either table>,
<tablename.non-distinct-fieldname>,
<tablename.some-other-field>
FROM
<tablename>
INNER JOIN – Two dashes in MSSQL are a comment, not sure about MySQL though, this inner join retrieves
– only data where the two or more specified fields match - if there is no match for a row
–in the second table the row will not be retrieved
<second-tablename>
ON <tablename.fieldname> = <second-tablename.fieldname> – The joining fields
WHERE – The where clause - remember this where happens ‘after’ the join
<tablename.fieldname> = ‘SomeValue’
So in your case something like
SELECT
order_header.od_id,
order_header.od_address1, – and address 2, address 3 etc etc
order_lines.quantity,
order_lines.linetotal,
items.description
INNER JOIN
order_header.od_id = order_lines.od_id
INNER JOIN
items on items.it_id = order_lines.it_id
WHERE order_header.od_id = 5
So this query will basically get the details of an order with an od_id of 5. It gets the line details and also the item description for each line item. Of course the lines MUST exist in the lines table or the order header won’t be returned. The item must also exist in the items table.
Read up on T-SQL and the different types of joins. I have to say that writing your own shopping cart with no real knowledge of SQL/PHP etc is no easy task. If you need any help I’ll help you out! 