This topic is locked

Adding parent and child record at same time

9/25/2007 1:44:29 PM
PHPRunner General questions
M
mrpeeble author

When adding a new parent record I want to create a child record at the same time.I want to be able to automatically insert the auto increment ID from the parent to the ParentID in the child table, as well as all other fields that match between the parent and child table.
Below is an example of the tables.
Eg.

Parent Table Child Table

ID ID

Employee Number ParentID

Surname SeniortyDate

GivenName SeniorityHours

SeniorityDate OfficeSeniorityDate

SenioirtyHours

OfficeSeniorityDate
From what I havei read in the forums the "After Add" functions needs to be used because the auto increment ID is only generated after the record is added?

J
Jane 9/26/2007

Hi,
yes you need to use After record added event for this purpose.

Here is a sample code:

global $conn;

$id = mysql_insert_id();

$strInsert = "insert into ChildTable (ParentID,SeniortyDate,SeniorityHours,OfficeSeniorityDate) values (".$id.", '".$values["Surname"]."', '".$values["GivenName"]."', '".$values["SeniorityDate"]."')";

db_exec($strInsert,$conn);

M
mrpeeble author 9/26/2007

Hi Jane:
I tried your sample code and It produced me an error. I'm posting my actual tables and the function I wrote.

Hopefully you'll be able to point out my error.
_stfstaffmd ( parent table)

ID

EmployeeNumber

Surname

GivenName

SeniorityDate

SeniorityHours

OfficeSeniorityDate

MyStatus

MyPosition

Address

ApartmentNumber

City

PostalCode

Telephone

SecurityAccessNumber



_stfstaffdetails (child table)


ID

StaffID

SeniorityDate

SeniorityHours

OfficeSeniorityDate

MyStatus

MyPosition

Address

ApartmentNumber

City

PostalCode

Telephone

SecurityAccessNumber
<?php

function AfterAdd(&$values)

{

global $conn;

$id = mysql_insert_id();

$strInsert = "insert into stf_staffdetails (StaffID,SeniorityDate,SeniorityHours,OfficeSeniorityDate,MyStatus,MyPosition, Address,ApartmentNumber,City,PostalCode,Telephone,SecurityAccessNumber) values (".$id.", '".$values["SeniorityDate"]."', '".$values["SeniorityHours"]."', '".$values["OfficeSeniorityDate"]."', '".$values["MyStatus"]."', '".$values["MyPosition"]."', '".$values["Address"]."', '".$values["ApartmentNumber"]."', '".$values["City"]."', '".$values["PostalCode"]."', '".$values["Telephone"]."', '".$values["SecurityAccessNumber"]."')";

db_exec($strInsert,$conn);
}

return true;

?>

So as before, I would like to be able to automatically insert the corresponding values in the parent table to the corresponding values in the child table with the autoincrement ID in the parent table to populate the StaffID field in the child table.
Thanks in advance.

Alexey admin 9/26/2007

Hi,
you need to enter the code on the Eventstab in PHPRunner.

Don't modify generated files.

M
mrpeeble author 9/26/2007

Hi Alexy:
I have also tried to add the event using the phprunner,

but what happens is that, instead of inserting the data into the child table, the added data is duplicated several times in the parent table.

M
mrpeeble author 9/26/2007

Hi Alexy:
Let me rephrase that, the error I get when I use phprunner to add the after record added event is:
Missing argument 1 for AfterAdd(), called in

D:\WebDev\OfficeL\STAFF_PHP4265Test\stf_staffmd_add.php on line 479 and defined

What does that mean?
Thanks

J
Jane 9/27/2007

Hi,
this error means that you've missed some arguments in the AfterAdd function.

I recommend you to add your code on the Events tab in the PHPRunner. In this case PHPRunner generates correct function header with all arguments.

M
mrpeeble author 9/27/2007

Hi Jane:
I did add the code using the Events tab in the PHPRunner, and that was the error it was giving me.

Would you be able to show me the full code based on my previous entries.
Thanks,

J
Jane 9/27/2007

Please publish your project on Demo Account and post a URL to your pages here or send it to [email=support@xlinesoft.com]support@xlinesoft.com[/email] along with instructions on reproducing this error.

I'll find what's wrong with your project inspecting it at Demo account site.

M
mrpeeble author 10/1/2007

Thanks for all your help Jane and Alexey.

Much appreciated.

D
drh 10/11/2007

Hi forum,

mrpeeble, did you get this resolved? I am getting the same error message. Could you post the code that did work? Many thanks,

Dave