This topic is locked

Copying child records

6/6/2008 3:16:14 AM
PHPRunner General questions
T
Tane author

Hi,
I was wondering what might be the best way to implement optionally copying child records when a master record is copied? A database procedure could do it but how to easiest call it? It would have to be called from the Add-page, I guess.
Tane

J
Jane 6/7/2008

Tane,
use Before record added event on the Events tab to copy detail records.

S
steveh 10/17/2008

Being stupid here, but how do I know the ID of the record it was copied from and the fact that it was a copy and not a new add from scratch?

J
Jane 10/20/2008

Steve,
ID of copied record is in the $_REQUEST["copyid1"] variable.

O
OLDmrcaseyman 11/11/2008

Could anyone enlighten a newbie?

I would like the detail records of a master all copied when the master is copied.

Could someone give an example of the code I would use in the event mentioned above please?

Thank you!

O
OLDmrcaseyman 11/13/2008

With Jane's help I have succeeded in copying the child records of a master when the master is copied.

Here's the solution that worked:
Event: Add Page, Before Process

if (@$_REQUEST["copyid1"])

$_SESSION["copyid1"] = $_REQUEST["copyid1"];
Event: Add Page, After Record Added

$str = "select order1, Catagory, Item_Description from t_options_selected where Order_ID =".$_SESSION["copyid1"];

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

while($data = db_fetch_array($rs))

{

$strInsert = "insert into t_options_selected (Order_ID, Catagory, Item_Description) VALUES ('".$values["Order_ID"]."','".$data["Catagory"]."','".db_addslashes($data["Item_Description"])."')";

db_exec($strInsert,$conn);

}
Notes:

I had lots of trouble with data that contained single and/or double quotes.

db_addslashes($data["field"]) solved half my problem

adding single quotes to query solved the other half.