This topic is locked

Updating a related field in master table when a record in a child tabl

8/6/2011 5:50:42 PM
PHPRunner General questions
P
pim author

When
my master table is 'e_bookings'

---------------------

BookingsID int

TotalAcc int
and my detail table 'h_acc_select'

-------------------

AccSelectID int

BookingsID

RoomPrice

NrOfNights
and the relation is set as Master (BookingsID) to Child (BookingsID)
and I use this code in events 'after record added' and 'after record updated':
-------------------------------------------------------------------------

global $conn;
$str = "select sum(RoomPrice * NrOfNights) from h_acc_select where BookingsID = '{$values["BookingsID"]}'";

$rs = db_query($str,$conn);

$data = db_fetch_numarray($rs);

$ttl=$data[0];

$strUpdate = "update e_bookings set TotalAcc= '{$ttl}' where BookingsID = '{$values["BookingsID"]}'";

db_exec($strUpdate,$conn);
-------------------------------------------------------------------------
the sum is updated in the master table once data in its child table has been added or edited.

However, when deleting the child data, the sum in the master table is not equally deleted.
Which code must be added where so that field 'TotalAcc' in the master table will return to zero when it's child in h_acc_select is deleted?
Thank you in advance for your help,
Pim