create a record in second table when first table is updated

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
shkdxb
Veteran Member
Posts: 40
Joined: 2013-06-28 18:18

create a record in second table when first table is updated

Post by shkdxb » 2013-06-29 14:29

hi

i want some help in hooks.

i have two table table1 and table2. i need table2 to get it updated with a new record from table1.field1 and table1.field2 with user name and userid. hope this can be done. i have gone through this forum & couldn't find a correct script. appreciate any help from forum members.

KSan
AppGini Super Hero
AppGini Super Hero
Posts: 252
Joined: 2013-01-08 20:17

Re: create a record in second table when first table is upda

Post by KSan » 2013-06-29 23:09

I think this can be do e in the after_insert hook of table1 with an SQL insert into table2 kind of line.

shkdxb
Veteran Member
Posts: 40
Joined: 2013-06-28 18:18

Re: create a record in second table when first table is upda

Post by shkdxb » 2013-06-30 03:57

Ksan,
thanks. yes this can be done by Tablename_after_insert hook. can anyone can give script for this? i am very new and don't know how to write this script.

shkdxb
Veteran Member
Posts: 40
Joined: 2013-06-28 18:18

Re: create a record in second table when first table is upda

Post by shkdxb » 2013-06-30 04:27

i have created a script going through some of the forum posts. can anybody tell me whether this will work?

function MainTable_after_insert($data, $memberInfo, &$args){

$ID = $data['ID'];
$Jobnumber = $data['Jobnumber'];
$Status = $data[‘Status’];
$withwhom = $data['withwhom'];
$Location = $data['Location'];
$ChangedBy = $memberinfo['username'];
$ChangedDate = $data['date'];

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("your_database_name") or die(mysql_error());

if ($username <> 'admin'){ //I trap to not record admin access as it is not relevant to the stats

mysql_query( "INSERT INTO StatusTracking (Jobnumber, Status, WithWhom, Location, ChangedDate, ChangedBy) VALUES ('$Jobnumber', '$Status', '$withwhom', '$ChangedBy', '$ChangedDate')") or die(mysql_error());
return TRUE;
}

User avatar
a.gneady
Site Admin
Posts: 1287
Joined: 2012-09-27 14:46
Contact:

Re: create a record in second table when first table is upda

Post by a.gneady » 2013-07-01 20:40

You don't need to use mysql_connect and mysql_select_db as there is already an open connection ready to use.
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Re: create a record in second table when first table is upda

Post by shasta59 » 2013-07-02 02:32

Ahmad

I was finding if I did not use the mysql_connect etc it sometimes did not work properly. Rather than figure out the reason for this I put this structure in to ensure a connection. I tried it both ways and could not find a reason for the error when it did not work but did not get an error when I used the mysql connect. As it happened so rarely but was most annoying when it did I went to forcing open the connection using the mysql_connect structure.

It is on my list to try and see a reason for it but it has a very low priority. Could be server side or some other reason.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

shkdxb
Veteran Member
Posts: 40
Joined: 2013-06-28 18:18

Re: create a record in second table when first table is upda

Post by shkdxb » 2013-07-02 03:57

I could able to make to work. but i have one problem.

the statement:

mysql_query( "INSERT INTO StatusTracking (Jobnumber, Status, WithWhom, Location, ChangedDate, ChangedBy) VALUES ('$Jobnumber', '$Status', '$withwhom', '$Location', '$ChangedDate', '$ChangedBy')") or die(mysql_error());

returns ID field instead actual data for "Status" field in main table. this field is configured as lookup field from another table and this lookup table has two columns, ID and Status. how to force mysql to insert correct column?

shkdxb
Veteran Member
Posts: 40
Joined: 2013-06-28 18:18

Re: create a record in second table when first table is upda

Post by shkdxb » 2013-07-02 09:11

done it1

The main table is saving the ID field only- not actual looked up value from lookup fields. pointed the StatusTracking to point to the same lookup table as MainTable. problem solved.

User avatar
toconnell
Veteran Member
Posts: 204
Joined: 2013-04-09 19:29
Location: Oklahoma City, OK
Contact:

Re: create a record in second table when first table is upda

Post by toconnell » 2013-11-19 19:11

Can you put in your final code? I would like to do the same thing. I am interested in your fix for the look up field. I cannot seem to get that part to work. Thanks, Tina
Tina O'Connell
Web Dev & Appgini FAN

stevelizardi
Posts: 5
Joined: 2014-02-27 20:47

Re: create a record in second table when first table is upda

Post by stevelizardi » 2014-04-25 12:39

Is there a tutorial on how to do this? I was trying to see if I could get it to work in just creating a app and reference a field, but it didnt work. Any help would be great.

Thanks!

User avatar
toconnell
Veteran Member
Posts: 204
Joined: 2013-04-09 19:29
Location: Oklahoma City, OK
Contact:

Re: create a record in second table when first table is upda

Post by toconnell » 2014-04-28 14:03

For the status to show up you need to put in a join to the foreign table..

INNER JOIN 'statustable' ON
statustable.record_id=.... you get the idea right?
Tina O'Connell
Web Dev & Appgini FAN

Post Reply