This topic is locked

User 'After record added' event to retrieve autoincremented ke

4/12/2007 8:12:24 PM
PHPRunner General questions
D
dlpirl author

After I add a record, I want to do something like the following using the 'After record added' event...
$str = "select CompanyID from ".$strTableName." where ".$where;

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

$_SESSION["CompanyID"] = $rs;
However, I get the error message 'Undefined variable: strTableName'.

I know similar logic and syntax works in the 'Before record updated' event, but I want to set a variable to the autoincremented value of CompanyID immediately after adding a record to the Company table. How can this be done?

J
Jane 4/13/2007

Hi,
try to use following event code:

function AfterAdd()

{

global $strTableName,$conn;

$str = "select LAST_INSERT_ID() from ".$strTableName."";

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

$data = db_fetch_numarray($rs);

$_SESSION["CompanyID"]= $data[0];

}

Alexey admin 4/13/2007

Hi,
there is an easier way to obtain last inserted ID.

Here is the code:

function AfterAdd()

{

$_SESSION["CompanyID"]= mysql_insert_id();

}

D
dlpirl author 4/13/2007

Hi,

there is an easier way to obtain last inserted ID.

Here is the code:



It works. Thanks! However...

I need to set the default value of CustomerID correctly when a new order is added depending on how the form is loaded. Currently the default is

@$_SESSION[$strTableName."_masterkey1"]



I want to do something like

if(@$_SESSION[$strTableName."_masterkey1"],@$_SESSION[$strTableName."_masterkey1"],@$_SESSION["CustomerID"])



but this gives me an error 'Parse error: parse error, unexpected T_IF in /home/sunnyclo/public_html/phprunner/orders_add.php on line 244'
I also tried> "if(".@$_SESSION[$strTableName."_masterkey1"].",".@$_SESSION[$strTableName."_masterkey1"].",".@$_SESSION["CustomerID"].")"



but this gives me> if(,,28)

for the CustomerID.
What is the correct syntax for this type of logic for a field default value?

Sergey Kornilov admin 4/13/2007

Donald,
you need to execute this code in some other place (Event) and assign the value to some Session variable.
After that you can use this Session variable as a default value.

D
dlpirl author 4/13/2007

Donald,

you need to execute this code in some other place (Event) and assign the value to some Session variable.
After that you can use this Session variable as a default value.

Thanks, I'll think about how to do it that way.

hanb 11/5/2007

Hi,

there is an easier way to obtain last inserted ID.

Here is the code:


I am trying to use this code in version 5.1 but get a syntax error on function.

How should the code look like as it seems to be in a sub
Han

J
Jane 11/6/2007

Han,
event headers were changed in the last version of PHPRunner.

You need to remove your event, create it again and then add this code:

$_SESSION["CompanyID"]= mysql_insert_id();