This topic is locked
[SOLVED]

  to write form data into another table

1/29/2011 4:08:20 PM
PHPRunner General questions
V
Vienna author

I have looked at various examples of using values in a form to do "something else" besides just write directly to the table the form is based on.
The code examples in the PHPRunner manual don't seem to cover my situation exactly but I tried to do something with them which is failing in the ADD Page "After records added".
I have a master record with fields that I want to populate with data from the ADD form. I would like to collect some values from the Form and pass them along after the detail table is written to the Master table. I want these fields to UPDATE, I guess, the master table in its matching field values. Kill two birds with one stone. The PHPRunner examples show INSERT used more than anything so I have tried with INSERT.
So once the Details table is written to in the very next action I want selected values, either from the filled out fields in the form AS it is submitted, or taken from the details table just after a new record is added. So, I know that the values, I presume are referred to differently depending on whether I am retrieving them from the form once it is filled out, or from the table record where this form is saved.
Here is what I have tried and I get error messages even though the PHPRunner Syntax checker say the Syntax is okay--
Events file "After records added" activated on ADD page.
//** Save new data in another table ****
global $conn,$strTableName;
$strSQLSave = "INSERT INTO pharmacy_address (contact_titel, contact_academic_titel,contact_first_name,contact_second_name,contact_phone,contact_email) values (";
$strSQLSave .= $values["contact_titel"].",";

$strSQLSave .= $values["contact_academic_titel"].",";

$strSQLSave .= $values["contact_first_name"].",";

$strSQLSave .= $values["contact_second_name"].",";

$strSQLSave .= $values["contact_phone"].",";

$strSQLSave .= $values["contact_email"];

$strSQLSave .= WHERE ($keys["pharmacy_nr"] == $values["pharmacy_nr"]);

$strSQLSave .= ")";

db_exec($strSQLSave,$conn);

"

So, that version is failing on the "Where" with this error "Fatal error: Call to undefined function: where()"


In the following version with a different "where" it now fails even earlier----



And this next version where I am trying to use the values that are being submitted from the form to go write to the "pharmacy_address" Master table is also FAILING nicely!


I even tried a version using $postvalues instead of $values but so far I cannot make anything happen but errors.
This Action is my latest but it fails....
global $conn;

$strSQLInsert = 'UPDATE pharmacy_address SET contact_titel=$values["contact_titel"],contact_academic_titel=$values["contact_academic_titel"],contact_first_name=$values["contact_first_name"], contact_second_name= $values["contact_second_name"],contact_phone= $values["contact_phone"],contact_email= $values["contact_email"] WHERE pharmacy_nr= $values["pharmacy_nr"]' ;

db_exec($strSQLInsert,$conn);
It produces things like this after telling me it has a problem with the SQL--

0. include/dbconnection.php:36 db_query 1. UPDATE pharmacy_address SET contact_titel=$postvalues["contact_titel"],contact_academic_titel=$postvalues["contact_academic_titel"],contact_first_name=$postvalues["contact_first_name"], `conta...;

  1. Resource id #10;

    1. include/dbconnection.php:47 db_exec 1. UPDATE pharmacy_address SET contact_titel=$postvalues["contact_titel"],contact_academic_titel=$postvalues["contact_academic_titel"],contact_first_name=$postvalues["contact_first_name"], `conta...;


  2. Resource id #10;
    [size="4"]Here is the array that does keep being written successfully to the details table[/size]
    These are the values from the Add page form that I am trying to retrieve and select some, but not all of them to write in a correctly sequentialed string to the same-named fields in the Master details table where the record with primary id "pharmacy_nr" matches the one submitted in this form.
  3. Array ( [detailer_id] => 1 [pharmacy_nr] => 40181983 [contact_titel] => 1 [contact_academic_titel] => 1 [contact_first_name] => Adam [contact_second_name] => Ant [contact_e...;



    I want the pharmacy_nr value in the Add form to be the condition for a lookup in the "pharmacy_address" table so that just that specific record is modified or updated with those "pharmacy_nr" values which already exist as primary id's for each doctor record.

    I would love an in-depth tutorial that shows how to take values from the Form itself when Saved or as it is being Submitted and save specific values to matching fields in another table where an associated record already exists.
    Thank you greatly.

V
Vienna author 1/30/2011

I have solved this problem-- putting some sql code into the Events of the Add page at "After Record added" with this:
global $conn;

$strSQLInsert = "UPDATE pharmacy_address SET contact_titel= '".$values["contact_titel"]."',

contact_academic_titel= '".$values["contact_academic_titel"]."',contact_first_name= '".$values["contact_first_name"]."',

contact_second_name= '".$values["contact_second_name"]."',contact_phone= '".$values["contact_phone"]."',

contact_email= '".$values["contact_email"]."' where pharmacy_nr= '".$values["pharmacy_nr"]."' " ; db_exec($strSQLInsert,$conn);

I had to try and try different ways, look at the errors and then try something else, but I could not find any example in the PHPRunner manual or Tips or Tutorials that showed me this-- and this is the ONLY way I have finally been able to write to my project database on a 1und1.de server.
It would be instructive from Sergey or Jane to explain why THIS works with the '" . and ."' & perhaps expand on how to solve such problems in PHPRunner.
It would be great if the next version of PHPRunner greatly expands on the Events code examples for such things as Updating specific fields with matching value types from other tables.
And which is the better approach?

To update another table directly from the POSTED VALUES of the form AS it processes, OR to wait until a details record has been written and THEN go retrieve the data fields and use that retrieval array to go update another table?
Thank you.