This topic is locked

Updating 2 tables from a single form

3/26/2007 12:05:24 PM
PHPRunner General questions
C
clawes author

I'm using phprunner v3.1 to develop an asset tracking db. Presently, I maintain 2 forms and 2 tables: one for the asset details and another for the warranty details for each asset. My boss wants me to place the fields for both tables on one form so that end users don't have to update 2 forms when adding an asset and its warranty info. In so doing, the application will be more intuitive and user-friendly.
Can anyone point me to the details on how to make this happen?
Thanks,

Conrad

J
Jane 3/27/2007

Conrad,
unfortunately you can't edit values from two or more tables on the EDIT page.
Here are two workarounds.

You can use BeforeEdit event to update values in the second table or AfterEdit event to redirect to the edit page of the second table.

C
clawes author 3/27/2007

The AfterEdit approach might be an acceptable compromise. The assets table is related to the warranty table via the asset_id field. The asset_id is the primary key in the assets table. When I create a warranty record, the related asset is selected from a dropdown (lookup) list. If I were to implement the AfterEdit approach as you suggested, how would get the asset_id pre-selected on the warranty form for the newly created asset?
To clarify, here are the steps to perform:
-Create new asset record

-Get the asset_id for the newly created record

-Open the warranty form to create a new record passing the asset_id as a parameter

-On the warranty form, the related asset should be pre-selected by default using the asset_id it received earlier.
The main code I need is how to get the asset_id of the new asset record. Anyone has a code snippet I can look at?

D
Dale 3/27/2007

In the After Add events of your asset record you could modify the snippet below to get your id.

I use the snippet.

Create New Customer

Get the new customer number and create a tysr_subscribers record for the customer. (It's the customer autoincremented number.)
If you didnt want the record pre created for you after adding the asset, then you could just enter the $_SESSION["last_inserted_row"] as the default value for your new warranty form and skip the insert record part.
global $conn;

$str2 = "select LAST_INSERT_ID() from `customer`"; // replace `customer` with your asset table name

$rs2 = db_query($str2,$conn);

$data2 = db_fetch_numarray($rs2);

$_SESSION["last_inserted_row"] = $data2[0]; // this will be the number assigned to the newly added asset record.
//** Insert a record into tysr_subscribers ****

$strSQLInsert = "insert into tysr_subscribers (customer_id, user_level, sub_date, allow_browser_signon, start_date) values (".$_SESSION["last_inserted_row"].",'Customer', now(), 1, now())";

db_exec($strSQLInsert,$conn);

return true;