This topic is locked

Need a field added automatically

10/2/2015 1:51:11 PM
PHPRunner General questions
S
stec5345 author

I have a table in which there is a field (int) I need to populate automatically based on a value from another table.
eg.

In my licenses table, I need the LicClientID field to auto populate with the ClientID from the physician_profile table where the inner joined licenses.LicPhysID = physician_profile.PhysID
I don't have to have the LicClientID on the ADD page. I just want the value to populate the field in the DB automatically for future queries
Any help available?

C
copper21 10/3/2015

You can update the other table on an "After record added" event. Go to the events page and go to the physician_profile table and go to the "Add" Page. From there, go to "After record Added" and use something like this:
$sql = "UPDATE licenses SET LicClientID = ".$values['ClientID']." WHERE LicPhysID = ".$values['PhysID']."";

CustomQuery($sql);
IF I am understanding you correctly, this should work.

S
stec5345 author 10/6/2015

Thanks Brian. I seem to get a syntax error however when running this stating that to check for correct syntax near the where statement:
Error type 256

Error description You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE LicPhysID =' at line 1

URL www.statmedcaredb.com/licenses_add.php?submit=1&fly=1&;

Error file C:\Inetpub\vhosts\statmedcaredb.com\httpsdocs\connections\MySQLiConnection.php

Error line 142

SQL query UPDATE licenses SET LicClientID = WHERE LicPhysID =
Any idea?



You can update the other table on an "After record added" event. Go to the events page and go to the physician_profile table and go to the "Add" Page. From there, go to "After record Added" and use something like this:
$sql = "UPDATE licenses SET LicClientID = ".$values['ClientID']." WHERE LicPhysID = ".$values['PhysID']."";

CustomQuery($sql);
IF I am understanding you correctly, this should work.

S
stec5345 author 10/6/2015

I think the logic needs to make this happen from an even on the licenses where when a license is added, it triggers the field within that table called LicClientID to be updated from the physician_profile table? Am I making sense?



Thanks Brian. I seem to get a syntax error however when running this stating that to check for correct syntax near the where statement:
Error type 256

Error description You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE LicPhysID =' at line 1

URL www.statmedcaredb.com/licenses_add.php?submit=1&fly=1&;

Error file C:\Inetpub\vhosts\statmedcaredb.com\httpsdocs\connections\MySQLiConnection.php

Error line 142

SQL query UPDATE licenses SET LicClientID = WHERE LicPhysID =
Any idea?

C
copper21 10/7/2015

The error is occurring because LicPhysID doesn't have a value...
SQL query UPDATE licenses SET LicClientID = WHERE LicPhysID = XXX?
There is nothing after LicPhysID meaning ".$values['PhysID']." does not have a value.
From the information that you provided, this is the example I was able to provide. Try it on the other table you were talking about on the ADD page.
Let me know if that helps. You can always provide me with your table structures for both and i can try to go from there.

A
Abul 10/23/2015



The error is occurring because LicPhysID doesn't have a value...
SQL query UPDATE licenses SET LicClientID = WHERE LicPhysID = XXX?
There is nothing after LicPhysID meaning ".$values['PhysID']." does not have a value.
From the information that you provided, this is the example I was able to provide. Try it on the other table you were talking about on the ADD page.
Let me know if that helps. You can always provide me with your table structures for both and i can try to go from there.


Hello!

I used this tips in my case where I want country (PK=id) to be populated automatically in State (FK="country_id) field. I use this code in Before record added event.

$sql = "UPDATE state SET country_id = ".$values['id']." FROM country WHERE countrynm = ".$values['country_name']."";

CustomQuery($sql);


But I got this error. Do you have any clue please?

Technical information

Error type 256

Error description You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM country WHERE countrynm = USA' at line 1

URL localhost/test/state_add.php?ferror=1&

Error file C:\xampp\htdocs\test\include\dbconnection.my.mysqli.php

Error line 63

SQL query UPDATE state SET country_id = FROM country WHERE countrynm = USA

C
copper21 10/24/2015

This statement doesn't look right...
Technical information

Error type 256

Error description You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM country WHERE countrynm = USA' at line 1

URL localhost/test/state_add.php?ferror=1&

Error file C:\xampp\htdocs\test\include\dbconnection.my.mysqli.php

Error line 63

SQL query UPDATE state SET country_id = FROM country WHERE countrynm = USA
It looks like ".$values['id']." doesn't have a value, therefore you are not seeing a value between country_id = & FROM. Also, the word FROM is not part of an UPDATE Statement as well.
I would be better able to help you if you give me the two table structures. Give me the table names and the fields in the tables, also noting the primary keys fore each...if you have primary keys.

A
Abul 10/24/2015



This statement doesn't look right...
Technical information

Error type 256

Error description You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM country WHERE countrynm = USA' at line 1

URL localhost/test/state_add.php?ferror=1&

Error file C:\xampp\htdocs\test\include\dbconnection.my.mysqli.php

Error line 63

SQL query UPDATE state SET country_id = FROM country WHERE countrynm = USA
It looks like ".$values['id']." doesn't have a value, therefore you are not seeing a value between country_id = & FROM. Also, the word FROM is not part of an UPDATE Statement as well.
I would be better able to help you if you give me the two table structures. Give me the table names and the fields in the tables, also noting the primary keys fore each...if you have primary keys.


@Brian21
Thank you so much for your cooperation. Here is my table structure.

  1. Country (id INT (PK), countrynm VARCHAR)
  2. State (sid INT (PK), state VARCHAR, country_id INT (FK))
    Thanks again.

A
Abul 10/24/2015

I think I have accomplished this by writing BEFORE INSERT trigger at db table. This following trigger give me the result exactly what I am looking for.

DROP TRIGGER countryid_update;

CREATE DEFINER=root@localhost TRIGGER countryid_update BEFORE INSERT ON state

FOR EACH ROW BEGIN

SET @country_id=1;

IF NEW.country_name IS NOT NULL THEN

SELECT country.id INTO @country_id FROM country WHERE countrynm=NEW.country_name;

SET NEW.country_id=@country_id;

END IF;

END;


When I submit state form with “State Name” and corresponding “Country_Name”, this trigger check the country table with the country_name and take the ‘id’ from country table and insert into the country_id field in state table.
But this trigger does not protect state table from potential sql injection attack. Because if I submit state form with only “state name” field data without “country_name” filed then still data can be inserted into state table without country_id field in the state table.
Now I am looking for trigger or event that actually block new record entry into state table if the country_name on the state form is mismatched or blank. Thanks for your wise contribution.

romaldus 11/3/2015



I have a table in which there is a field (int) I need to populate automatically based on a value from another table.
eg.

In my licenses table, I need the LicClientID field to auto populate with the ClientID from the physician_profile table where the inner joined licenses.LicPhysID = physician_profile.PhysID
I don't have to have the LicClientID on the ADD page. I just want the value to populate the field in the DB automatically for future queries
Any help available?


On After Record Addedevent Try This :



global $conn;

$strSQLUpdate = "UPDATE licenses SET LicClientID = (SELECT ClientID FROM physician_profile WHERE LicPhysID = ".$values['PhysID'].")";

db_exec($strSQLInsert,$conn);
A
Abul 11/3/2015



On After Record Addedevent Try This :



global $conn;

$strSQLUpdate = "UPDATE licenses SET LicClientID = (SELECT ClientID FROM physician_profile WHERE LicPhysID = ".$values['PhysID'].")";

db_exec($strSQLInsert,$conn);



This code is good for Add/Edit pages but could you please let us know how can we use similar code for similar purpose in case we import new records from Excel File. There is no After Insert Record event on import_page.php. I appreciate your contributions.

romaldus 11/3/2015



This code is good for Add/Edit pages but could you please let us know how can we use similar code for similar purpose in case we import new records from Excel File. There is no After Insert Record event on import_page.php. I appreciate your contributions.


There is After Importevent in import page

A
Abul 11/4/2015



There is After Importevent in import page


I don't see After Import Page. I see After Import Finished event on Import Page which is not functioning as After Added Record event on Add Page. Thanks for your quick reply.