This topic is locked

How to update last record added to database?

10/28/2007 3:58:21 PM
PHPRunner General questions
R
runnerPHP2008 author

After adding a record to the database, I want to add the Date/Time to that same record. Below I have code that tries to list the last record added as a parameter in the SQL statement, But it does not work. Do you see a scripting error in my code?
table name: _Models

field name: Login

field name: Model_ID (incrementing Primary Key)
global $conn;

$sql="Update _Model Set Login =now() where

Model_ID=(SELECT TOP 1 Model_ID

FROM _Models

ORDER BY Model_ID DESC)"

db_exec($sql,$conn);

Sergey Kornilov admin 10/28/2007

I recommend to use AfterAdd event that accept the key column value as a parameter.
function AfterAdd(&$values,&$keys,$inline)

{
global $conn;

$sql="Update _Model Set Login =now() where Model_ID = " . $keys["Model_ID"];

db_exec($sql,$conn);
}

R
runnerPHP2008 author 10/28/2007

I recommend to use AfterAdd event that accept the key column value as a parameter.

function AfterAdd(&$values,&$keys,$inline)

{
global $conn;

$sql="Update _Model Set Login =now() where Model_ID = " . $keys["Model_ID"];

db_exec($sql,$conn);
}


I posted exactly this code:
function AfterAdd(&$values,&$keys,$inline)

{
global $conn;

$sql="Update _Model Set Login =now() where Model_ID = " . $keys["Model_ID"];

db_exec($sql,$conn);
}
and I got an error:
Error Type: 2

Error description: Missing argument 1 for afteradd()
any suggestions?

Sergey Kornilov admin 10/28/2007

PHPRunner adds function header for you so you only need to post the following:
global $conn;

$sql="Update _Model Set Login =now() where Model_ID = " . $keys["Model_ID"];

db_exec($sql,$conn);
Please note that this will work in PHPRunner 4.1only.