This topic is locked

Adding/Subtracting Question

10/31/2008 11:45:12 AM
PHPRunner General questions
G
Greeham author

Hi,
Some time ago Jane helped me out with an Before record Added event to update one of tables and it read:-
update pilots set hours=hours+".$values["duration"]." where pilotid='".$values["pilotid"]."'
The problem I have is that if that vlaue is then edited it will always add it again as opposed to adjust it. does that make sense?
So basically, I would like in the first instance the value to be added but would like it to be adjustable through the before record added in the edit page. How would I allow adjustment both positive and negative once the edit has been completed?
Example:-
If I add a new reocrd and submit 3 for the duration, the pilots hours are then credited by 3.
If I want to then edit that record to read 2, I then submit that and the record then becomes 5 rather than amend it to 2
Should I use negative numbers to reduce the pilots hours amount?
Thanks,

Sergey Kornilov admin 10/31/2008

You need to apply different logic in BeforeEdit event. You need to find the difference between new and old value and apply it to Hours field.
hours = hours + $values["duration"]-$oldvalues["duration"]
One more thing. It would reasonable to implement BeforeDelete event as well with the following logic:
hours = hours - $values["duration"]