This topic is locked

Insert To child table with 'Before Edit' event

4/5/2007 5:12:38 PM
PHPRunner General questions
G
GeddyLee author

Using version 3.1

Build 197
Hello all,
I've got a parent child relationship set up for one of my tables.
I would like to write to the child table every time a user makes a change to the parent. I am currently trying to do this using the Before Edit event, however the problem I am running into is grabbing the ID of the parent table. Naturally if I do not have the parent's ID, the child will not know who it belongs to.
Obviously I don't want to include the Id on the parent's edit page because I don't want the users changing it, so I cannot get it with the events $values array. I tried to be sneaky and included the parent Id on the edit page but made it a "read only". However, this still causes an SQL error that leads me to believe that the element in the $values array for the Id is not being set.
I haven't played around too much with it, but before I waste a lot of time guessing, I was wondering if Anyone knew a surefire way to grab the Id AND not allow users to edit it.
Below is my code for the event:
[codebox]function BeforeEdit(&$values, $where)

{
// Parameters:

// $values - Array object.

// Each field on the Edit form represented as 'Field name'-'Field value' pair

// $where - string with WHERE clause pointing to record to be edited
//** Insert a record into another table ****

global $conn;

$user = $_SESSION['user'] //just my own security session.

$strSQLInsert = "insert into child (parent_Id, words) values (".$values['Id'].", 'This vehicle record was last updated by: ".$user." on: ".now()."' )";

db_exec($strSQLInsert,$conn);
return true;
// return true if you like to proceed with editing this record

// return false in other case
}[/codebox]
Maybe I'm just making a mistake in my code.
My next idea would be to strip apart the $where string to grab the Id from there, but i haven't taken a look to see what it looks like, I assume (just by guessing, not based on anything rational) it's something like "Where Id = 45", again if anyone knows off the top of their head that'd be great.
Let me know if I'm being clear enough.
Thanks,

-Brent

J
Jane 4/6/2007

Brent,
to pull parent ID from $where variable use following code:

$id = substr(strstr($where,'='),1);



And then use $id in your event code.

G
GeddyLee author 4/9/2007

Brent,

to pull parent ID from $where variable use following code:
And then use $id in your event code.


Great, thanks, that'll do just fine.