This topic is locked

AfterAdd event

10/11/2007 5:28:37 PM
PHPRunner General questions
D
drh author

Hi forum members,

Been quite awhile since I have visited, but now I have been asked to enhance some of the features in my app and I am running into a problem that seems to be quite common, yet I can't find an answer that works for me. It maybe a version incompatiblity.
First of all, this application was created using PHPR 3.1. I know, I am living in the dark ages, but just don't have the time to come up to speed on the newest PHPR version. I have made a few changes to the generated code over the years, so regenerating the code with PHPR is not really an option.
What I am trying to do is an after thought, but it seems very simple and I think it is. I have a field name Principal. I have added another field named O_Principal. What I need is to populate O_Principal with the same contents as Principal when the record is first created (added). O_Principal is hidden from the add page and it is not an editable field. I want it to remain the same as long as the record exists. I will use the OPrincipal field for some comparsions later in my application.
What I decided was that this would be a super place to use the AfterAdd event. Please correct me if I am wrong. So I manually created an AfterAdd function in ../includes/
..events.php with these contents:
function AfterAdd (&$values){

global $conn;

$strSQLInsert = " insert into mos_pcmacct (O_Principal) values ('".$values["Principal"]."')";

db_exec($strSQLInsert,$conn);

}
I reviewed many posts and this seemed to be the proper way to do this. Maybe since I am using PHPR 3.1, this will not work. I get an error when I add new records. The error is the typical "Missing argument 1 for afteradd()" message. Can anyone see my error? Is this code compatible with PHPR 3.1? Anyone have any suggestions?
Thanks in advance.

Dave

G
gawde 10/11/2007

Hi forum members,

Been quite awhile since I have visited, but now I have been asked to enhance some of the features in my app and I am running into a problem that seems to be quite common, yet I can't find an answer that works for me. It maybe a version incompatiblity.
First of all, this application was created using PHPR 3.1. I know, I am living in the dark ages, but just don't have the time to come up to speed on the newest PHPR version. I have made a few changes to the generated code over the years, so regenerating the code with PHPR is not really an option.
What I am trying to do is an after thought, but it seems very simple and I think it is. I have a field name Principal. I have added another field named O_Principal. What I need is to populate O_Principal with the same contents as Principal when the record is first created (added). O_Principal is hidden from the add page and it is not an editable field. I want it to remain the same as long as the record exists. I will use the OPrincipal field for some comparsions later in my application.
What I decided was that this would be a super place to use the AfterAdd event. Please correct me if I am wrong. So I manually created an AfterAdd function in ../includes/
..events.php with these contents:
function AfterAdd (&$values){

global $conn;

$strSQLInsert = " insert into mos_pcmacct (O_Principal) values ('".$values["Principal"]."')";

db_exec($strSQLInsert,$conn);

}
I reviewed many posts and this seemed to be the proper way to do this. Maybe since I am using PHPR 3.1, this will not work. I get an error when I add new records. The error is the typical "Missing argument 1 for afteradd()" message. Can anyone see my error? Is this code compatible with PHPR 3.1? Anyone have any suggestions?
Thanks in advance.

Dave


Hello Dave,
If your intention is to populate O_Principal with Principal after the record is added, then you need to UPDATE the record not insert. To do that, you will also need a WHERE clause to point to the correct record. for instance:
$lastid = mysql_insert_id($conn); // gives you the key of the record just addded //
$strSQLInsert = " UPDATE mos_pcmacct set O_Principal ='".$values["Principal"]."' where recordkey =". $lastid;

// quote $lastid if needed //
Hope that helps.
greg

Sergey Kornilov admin 10/12/2007

Dave,
AfterAdd event didn't have any parameters till version 4.1.

You need to upgrade to PHPRunner 4.1 in order to make it work.

G
gawde 10/12/2007

Dave,
Sergey is right, you don't have the $value available in your version. I think my suggestion will still work but with a bit more code. Add a SELECT to get the Position info, right after the function to get the last key (including the WHERE). Then, in the UPDATE sql statement, replace the $value with whatever "$data" variable you used in the SELECT.
$lastid = mysql_insert_id($conn); // gives you the key of the record just addded //
$strSQL1 = " SELECT Principal from mos_pcmacct where recordkey =". $lastid;

$rs1 = db_query($strSQL1, $conn);

$data1 = db_fetch_array($rs1);
$strSQLInsert = " UPDATE mos_pcmacct set O_Principal ='".$data1["Principal"]."' where recordkey =". $lastid;

// quote $lastid if needed //
Good luck.
Greg

D
drh author 10/12/2007

Thanks for the posts guys. I was wondering if it was a problem with the version of PHPR I was running. Thank you Sergey for pointing that out. Any new apps I create will be done in PHPR 4.1. Unfortunately, I am kind of stuck with the app I am running. Over the last 1 1/2 years I have had to enhance and customize to meet my clients needs. Therefore, the current code could not be regenerated easily. I understand that a lot has changed in the newer versions, and I can hardly wait to try it out.
I followed Greg_de's advice and got it to work with just a bit of tinkering. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=22202&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' /> Thank you very much Greg! This is the code that worked:
function AfterAdd(){

global $conn;

$lastid = mysql_insert_id($conn); // gives you the key of the record just addded //

$strSQL1 = " SELECT Principal from mos_pcmacct where seq =". $lastid;

$rs1 = db_query($strSQL1, $conn);

$data1 = db_fetch_array($rs1);

$strSQLInsert = " UPDATE mos_pcmacct set O_Principal ='".$data1["Principal"]."' where seq =". $lastid;

// quote $lastid if needed //

db_exec($strSQLInsert,$conn);
return true;

}
Once again, thanks guys for the help. This really is the most helpful forum I have ever been associated with. PHPR ROCKS!
Dave