This topic is locked

Updating details records when Master table record is updated

8/12/2025 9:23:36 PM
PHPRunner General questions
B
bioman author

Hi,
I've been unsuccessfully trying to do this for several days now but it hasn't worked. I'm using PHPRunner 11. I have a master-details relationship between two tables. I have 6 link fields: email, gamename, subject, topic, subtopics and id (linked to gamename_id in the details table). The master table is called gamenames and the details table is called partygames. If I make edits to a record in gamenames, I want the details to reflect those same edits. I have tried several methods to accomplish this in the After Record Updated event for gamenames. None of these have worked but I'm not sure why. The values will change on the gamenames (master table), but none of the child records get updated.

Method 1:

// Update partygame records based on the master record's ID
$sql = "UPDATE partygames
SET email='" . db_addslashes($values["email"]) . "',
gamename='" . db_addslashes($values["gamename"]) . "',
subject='" . db_addslashes($values["subject"]) . "',
topic='" . db_addslashes($values["topic"]) . "',
subtopics='" . db_addslashes($values["subtopics"]) . "'
WHERE gamename_id='" . db_addslashes($values["id"]) . "'";

DB::Exec($sql);

Method 2:

// In the After record Updated event for gamenames

// Update partygame records based on the master record's ID
$sql = "UPDATE partygames
SET email='" . db_addslashes($values["email"]) . "',
gamename='" . db_addslashes($values["gamename"]) . "',
subject='" . db_addslashes($values["subject"]) . "',
topic='" . db_addslashes($values["topic"]) . "',
subtopics='" . db_addslashes($values["subtopics"]) . "'
WHERE gamename_id='" . db_addslashes($oldvalues["id"]) . "'";

DB::Exec($sql);

Method 3:

// In the After record Updated event for the master table 'gamenames'

// Define the new values to update in the details table 'partygames'
$fieldValues = array();
$fieldValues["email"] = $values["email"];
$fieldValues["gamename"] = $values["gamename"];
$fieldValues["subject"] = $values["subject"];
$fieldValues["topic"] = $values["topic"];
$fieldValues["subtopics"] = $values["subtopics"];

// Define the WHERE clause using the master record's ID
$keyValues = array();
$keyValues["gamename_id"] = $values["id"];

// Execute the update on the details table
DB::Update("partygames", $fieldValues, $keyValues);

return true;

None of these work and I'm not sure what I'm doing wrong. Does anyone have any insight?

Tandy 8/13/2025

On the table you are editng in the After record updated:

$data = array();
$keyvalues = array();
$data["email"] = $values["email"];
$data["gamename"] = $values["gamename"];
$data["subject"] = $values["subject"];
$data["topic"] = $values["topic"];
$data["subtopics"] = $values["subtopics"];
$keyvalues["gamename_id"] = $values["gamename_id"];
DB::Update("TABLE TO BE UPDATED", $data, $keyvalues );

Hope that helps..

B
bioman author 8/13/2025

Thanks Tandy,
It still isn't updating the value though. The master table is called gamenames and the details table is called partygames. The changes get made on the master table but not on the details records (partygames table). This is the code I'm using:

$data = array();
$keyvalues = array();
$data["email"] = $values["email"];
$data["gamename"] = $values["gamename"];
$data["subject"] = $values["subject"];
$data["topic"] = $values["topic"];
$data["subtopics"] = $values["subtopics"];
$keyvalues["gamename_id"] = $values["gamename_id"];
DB::Update("partygames", $data, $keyvalues );
return true;

It seems basically the same as Method 3 (which didn't work either), but with $data replacing $fieldValues.

It seems like the code is correct for both of these but nothing happens to the details. Are there other things besides the code itself that might cause these failures? For example, my project is connected to 2 databases, but gamenames and partygames are in the same database, so it doesn't seem like this should matter. Or is something missing in the code? Any further insight would be great. Thanks,
Brett

Tandy 8/13/2025

That code worked for mine. Was hoping that it would work for you. You are using case sensitive right? It is in After Record Updated as well right? Then in mine I do not have return true.

Here is mine. I have it in my fuel(After Record Updated) and need to update my Mileage in Truck Profile:

//Update Mileage in truck_profile - begin
$data = array();
$keyvalues = array();
$data["odometer"] = $values["odometer"];
$keyvalues["truck_number"] = $values["truck_number"];
DB::Update("truck_profile", $data, $keyvalues );
//Update Mileage in truck_profile - end

Tandy 8/13/2025

Here is another one taking my ending milage of my trip report and updating my mileage on my Truck Lube. Both tables have the same Key Values of the Truck Number. Not going for the ID. Again this is listed in my Trip Report under After Record Updated and still no return True;

//Update Mileage in truck_lube - begin
$data = array();
$keyvalues = array();
$data["mileage"] = $values["end_odometer"];
$keyvalues["truck_number"] = $values["truck_number"];
DB::Update("truck_lube", $data, $keyvalues );
//Update Mileage in truck_lube - end

B
bioman author 8/14/2025

Thanks,
I tried removing the return true line, but this didn't have an effect. I checked the cases and they all match correctly. I thought maybe the issue was that the master table uses "id" for the field that links to "gamename_id" in the details table so I edited the code as follows:

// In the After record Updated event for the master table 'gamenames'

// Define the new values to update in the details table 'partygames'
$data = array();
$keyvalues = array();
$data["email"] = $values["email"];
$data["gamename"] = $values["gamename"];
$data["subject"] = $values["subject"];
$data["topic"] = $values["topic"];
$data["subtopics"] = $values["subtopics"];
$keyvalues["gamename_id"] = $values["id"];
// Execute the update on the details table
DB::Update("partygames", $data, $keyvalues );

This didn't work but I'm wondering if maybe this is on the right track...

Is the syntax still wrong?

C
cristi 8/14/2025

Instead of doing this in PHP I strongly advise to use a foreign key constraint in the database.
You gain not only the functionality you want but also consistency checks for your tables.
Always define foreign keys to maintain valid relationships between tables. Without foreign keys, orphaned rows may occur, leading to data inconsistency.

B
bioman author 8/14/2025

Hi Cristi,
I'm not very familiar with this approach, but I have heard of it through AI troubleshooting. Is there anything I need to do in PHPRunner to make this work or is it just done through PHP MyAdmin? Do you have any advice on how to go about this? Thanks,
Brett

C
cristi 8/15/2025

https://youtu.be/oT4YWW_sbNQ?feature=shared

Be aware that foreign key constraints are supported ONLY on InnoDB for both MySQL and its most popular fork MariaDB.