This topic is locked

Record History on Certain Field

11/4/2007 6:02:59 PM
PHPRunner General questions
M
mkyle1 author

Hi,
I am trying to setup the below:
When user edits a certain field in table1 it copy's the previous value of that field to table2 with other fileds from table1. I would also like to add a time added to table2.
Please can anyone help
Thanks, Martin

Sergey Kornilov admin 11/5/2007

Very easy - use BeforeEdit event and sample action "Insert a record into another table".
Use now() function to insert the current datetime.

M
mkyle1 author 11/6/2007

Hi,
please can you provide me an example and I will give it ago
Thanks, Martin

J
Jane 11/6/2007

Martin,
you can find sample code in the PHPRunner Help -> Advanced topics > Events -> Predefained actions -> Insert a record into another table.

M
mkyle1 author 11/6/2007

Hi,
thanks will give it ago this evening.
Thanks again
Martin

M
mkyle1 author 11/7/2007

Hi,
going round in circles on how to get this working. Please can you advise if the code below is correct.
Thanks, Martin
New table name = emp_history with the below fields.
id

fName

sName

current_emp

urn
Table to copy only the below fields field current_emp is modified
table name = driver
fName

sName

current_emp

urn
[codebox]
// Parameters:

// $values - Array object.

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

// $where - string with WHERE clause pointing to record to be edited

// $oldvalues - Array object with existing data record content

// $keys - Array object with added record key column values
//** Save old data record in another table ****

global $conn,$strTableName;
$strSQLSave = "INSERT INTO emp_history (fName, sName, current_emp, urn) SELECT fName, sName, current_emp, urn FROM ".$strTableName." where ".$where;

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

// return false otherwise[/codebox]

Sergey Kornilov admin 11/7/2007

Any error messages?
A general advise - print SQL query on the page to make sure it looks and works good:
echo $strSQLSave;

M
mkyle1 author 11/13/2007

Hi,
got it working with the above. Please can you advise how to only save to the second table if only field current_emp is changed.

[codebox]
// Parameters:

// $values - Array object.

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

// $where - string with WHERE clause pointing to record to be edited

// $oldvalues - Array object with existing data record content

// $keys - Array object with added record key column values
//** Save old data record in another table ****

global $conn,$strTableName;
$strSQLSave = "INSERT INTO emp_history (fName, sName, current_emp, urn) SELECT sName, fName, current_emp, urn FROM ".$strTableName." where ".$where;

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

// return false otherwise
[/codebox]
Thanks again
Martin

J
Jane 11/15/2007

Martin,
try to use this code:

//** Save old data record in another table ****

global $conn,$strTableName;

if ($values["current_emp"]!=$oldvalues["current_emp"])

{


$strSQLSave = "INSERT INTO emp_history (fName, sName, current_emp, urn) SELECT sName, fName, current_emp, urn FROM ".$strTableName." where ".$where;

db_exec($strSQLSave,$conn);

}

return true;

M
mkyle1 author 11/19/2007

Martin,

try to use this code:


Hi,
tried the above but still copies the record to the other table even though I have no updated the current_emp. Please can you advise what is wrong
Thanks, Martin

J
Jane 11/20/2007

Martin,
try to print and compare old and new values on the page:

//** Save old data record in another table ****

global $conn,$strTableName;

echo "old value: ".$oldvalues["current_emp"];

echo "
";

echo "new value: ".$values["current_emp"];

M
mkyle1 author 11/20/2007

Martin,

try to print and compare old and new values on the page:


Hi,
tried as you suggested but not joy. I guess I must be doing something daft! Code below I used:
[codebox]function BeforeEdit(&$values, $where, &$oldvalues, &$keys,&$message,$inline)

{
/ Parameters:
// $values - Array object.
// Each field on the Edit form is represented as a 'Field name'-'Field value' pair
// $where - string with WHERE clause pointing to record to be edited
// $oldvalues - Array object with existing data record content
// $keys - Array object with added record key column values
//** Save old data record in another table ****
global $conn,$strTableName;

echo "old value: ".$oldvalues["current_emp"];

echo "
";

echo "new value: ".$values["current_emp"];

if ($values["current_emp"]!=$oldvalues["current_emp"])

{

$strSQLSave = "INSERT INTO emp_history (fName, sName, current_emp, urn) SELECT sName, fName, current_emp, urn FROM ".$strTableName." where ".$where;

db_exec($strSQLSave,$conn);

}

return true;
// return true if you like to proceed with editing this record

// return false otherwise
} // function BeforeEdit[/codebox]
Thanks again
Martin

J
Jane 11/21/2007

Martin,
it's difficult to tell you what's happening without seeing actual files.
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
mkyle1 author 11/25/2007

Martin,

it's difficult to tell you what's happening without seeing actual files.
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.


Hi,
setup a demo account and dropped you an email with how to recreate the issue
Thanks, Martin