Hello,
I'm having some trouble coding my PHP administration application. I want to be able to log customer orders, and payments made by our customers, in a browser-based admin app.
A key part of this is the ability to calculate the grand total of their order, and compare this against payments made, to see if they still owe money.
Rather than use an SQL query every time I want to calculate a grand total, I want to save the grand total in the Orders table alongside the order ID, date etc...
Let's simplify and say I have two tables:
--Orders--
Order ID (PK)
Cust ID (FK)
Date
Grand Total
--Line Items--
Line ID (PK)
Order ID (FK)
Model
Price
I insert line items with a simple HTML form.
In a seperate form, I manually set the Price of each line item with an SQL UPDATE command. Also in this form is a hidden field, dynamically filled by an SQL query which calculates the grand total of all line items. On form submission, the line item receives its Price, and the form also updates the Grand Total in the Orders table using the hidden field.
The problem: When I set the price, the Grand Total is calculated and set before the Price field is updated. This means it's always one step behind. If I send another UPDATE command without changing anything, of course this corrects the problem. But it's hardly foolproof.
Hope this makes sense! How can I ensure that Grand Total is always accurate, without having to submit forms twice?