This topic is locked
[SOLVED]

 View data from one table in another

8/15/2019 1:30:33 AM
PHPRunner General questions
A
AndrewMark author

I have two tables, Table A and Table B. Table A has several fields already populated for User One. When User One wants to do an Addnew for Table B I would like a few of the populated data from Table A to be automatically visible on the Addnew form for Table B. I have tried all kinds of join queries and event codes but cannot get it working. Any assistance would be appreciated.

A
acpan 8/15/2019

Probably no need to do join query, suggestion:
In App init (After Application Initialised) event:
Query the data from Table A using the User ID and store in SESSION variables.
At the Add new form for Table B, assigned the field values with the session variables.
ACP

A
AndrewMark author 8/15/2019



Probably no need to do join query, suggestion:
In App init (After Application Initialised) event:
Query the data from Table A using the User ID and store in SESSION variables.
At the Add new form for Table B, assigned the field values with the session variables.
ACP



Thanks, have tried to code this but could not get it working.

A
acpan 8/15/2019

It has to work, this is pretty straight forward. Can't help by reading your "stories".

Sergey Kornilov admin 8/15/2019

Your description is not very clear. I don't think a JOIN can help here. You are on the Add page, the record doesn't exist yet and there is nothing to join with.
Here is an event you can use to populate fields on Add page load:

https://xlinesoft.com/phprunner/docs/process_record_values.htm
Pull data from table A and assign value to some of table B fields.

A
acpan 8/16/2019

Try this: First, don't do any join for your SQL query. i.e. table A and table B are seperated.
In process record values event of table B's Addnew form:
// Get table A values with $_SESSION["your_user_id"]
$rs = CustomQuery("select Afield1, Afield2 from tableA where user_id = ".$_SESSION["your_user_id"]);

$data = db_fetch_array($rs);
// check if you get the right data, disable in production

echo json_encode($data);
// Assign table A values to table B fields

$values['Bfield5'] = $data["Afield1"];

$values['Bfield3'] = $data["Afield2"];
// When click addnew for table B, the values from Table A will be shown on table B's form.

A
AndrewMark author 8/16/2019



Try this: First, don't do any join for your SQL query. i.e. table A and table B are seperated.
In process record values event of table B's Addnew form:
// Get table A values with $_SESSION["your_user_id"]
$rs = CustomQuery("select Afield1, Afield2 from tableA where user_id = ".$_SESSION["your_user_id"]);

$data = db_fetch_array($rs);
// check if you get the right data, disable in production

echo json_encode($data);
// Assign table A values to table B fields

$values['Bfield5'] = $data["Afield1"];

$values['Bfield3'] = $data["Afield2"];
// When click addnew for table B, the values from Table A will be shown on table B's form.



Thanks, I inserted the code but when trying an Addnew in Table B received a type 256 error. I have since replaced CustomQuery with DB::Query and the type 256 error is not returning. But unfortunately the table A field values are still not appearing in table B form when I do an add new nor do I see any echo message appearing. The code I have used is per below. OrderID is the field name of the field in both table A and table B that the values of which need to match.
// Get table A values with $_SESSION["your_user_id"]

$ID=$values["OrderID"];

$rs = DB::Query("select EntityName, EntityType, ABN, OrderID from orders where OrderID = ".$_SESSION['$ID']);

$data = db_fetch_array($rs);
// check if you get the right data, disable in production

echo json_encode($data);
// Assign table A values to table B fields

$values['RequestedEntityName'] = $data["EntityName"];

$values['RequestedEntityType'] = $data["EntityType"];

$values['RequestedEntityABN'] = $data["ABN"];
// When click addnew for table B, the values from Table A will be shown on table B's form.

A
acpan 8/17/2019

Hi,
Your $_SESSION['$ID'] is not defined. You can just use $ID directly.
Try this:
In process record values event of table B's Addnew form:
// Get orders table's values with OrderID

$ID=$values["OrderID"];

$sql = "select EntityName, EntityType, ABN, OrderID from orders where OrderID = $ID";

$rs = DB::Query($sql);

$data = db_fetch_array($rs);
// check if you get the right data and if SQL is valid, disable in production

echo "SQL=$sql | Result=".json_encode($data);
// Assign table A values to table B fields

$values['RequestedEntityName'] = $data["EntityName"];

$values['RequestedEntityType'] = $data["EntityType"];

$values['RequestedEntityABN'] = $data["ABN"];
// check if you data assigned properly, disable in production

echo "

Assign Values: RequestedEntityName =".$values['RequestedEntityName']." | RequestedEntityType =".$values['RequestedEntityType']." | RequestedEntityABN =".$values['RequestedEntityABN'];

A
AndrewMark author 8/17/2019



Hi,
Your $_SESSION['$ID'] is not defined. You can just use $ID directly.
Try this:
In process record values event of table B's Addnew form:
// Get orders table's values with OrderID

$ID=$values["OrderID"];

$sql = "select EntityName, EntityType, ABN, OrderID from orders where OrderID = $ID";

$rs = DB::Query($sql);

$data = db_fetch_array($rs);
// check if you get the right data and if SQL is valid, disable in production

echo "SQL=$sql | Result=".json_encode($data);
// Assign table A values to table B fields

$values['RequestedEntityName'] = $data["EntityName"];

$values['RequestedEntityType'] = $data["EntityType"];

$values['RequestedEntityABN'] = $data["ABN"];
// check if you data assigned properly, disable in production

echo "

Assign Values: RequestedEntityName =".$values['RequestedEntityName']." | RequestedEntityType =".$values['RequestedEntityType']." | RequestedEntityABN =".$values['RequestedEntityABN'];


Thank you, worked perfectly!!