CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 INSERT INTO with a subquery (passing foreign key)

Print topic Send  topic

Author Message
andy


Posts: 183
Posted: 09/25/2008, 2:26 AM

Hi

I am trying to copy
a) one record first from a temporary master table to a master table with the same structure and then b) multiple records from a temporary child table to a child table with the same structure

Here's a simplified version of my code which is executed
Validation checks on BeforeExecuteUpdate

This code in After ExecuteUpdate of master form:

This is the simple version but it doesn't, of course, obtain the PrimaryKey from the newly created record to pass it as the ForeignKey in the newly created child tables so they aren't linked.
  
global $masterform;  
  
$db = new clsDBConnection1();  
$PID = $masterform->PID->GetValue();  //Gets the primary key field from the form  
$result = $db->query("INSERT INTO mastertable (field1, field2, field3) SELECT field1, field2, field3 FROM mastertable_temp WHERE PRIMARYKEY = $PID");  
  
// so far so good. That works fine. The autoincrement ID field is automatically created by the way  
  
$LastID = CCDLookUp("LAST_INSERT_ID()", "mastertable","LAST_INSERT_ID()", $db); // Gets the newly created primary key to pass as the foreign key  
$result2 = $db->query("INSERT INTO childtable (foreignkey, fieldA, fieldB, fieldC)   
SELECT fieldA, fieldB, fieldC FROM childtable_temp WHERE foreignkey = $PID");  
$db->close();  

Building on the above this is where I am stuck in trying to obtain the newly created primary key ("LAST_INSERT_ID()") and passing it to the child table with a subquery.

  
global $masterform;  
  
$db = new clsDBConnection1();  
$PID = $masterform->PID->GetValue();  //Gets the primary key field from the form  
$result = $db->query("INSERT INTO mastertable (field1, field2, field3) SELECT field1, field2, field3 FROM mastertable_temp WHERE PRIMARYKEY = $PID");  
  
// so far so good. That works fine. The autoincrement ID field is automatically created by the way  
  
$LastID = CCDLookUp("LAST_INSERT_ID()", "mastertable","LAST_INSERT_ID()", $db); // Gets the newly created primary key to pass as the foreign key  
$result2 = $db->query("INSERT INTO childtable (foreignkey, fieldA, fieldB, fieldC)   
VALUES($LastID ,(SELECT fieldA, fieldB, fieldC FROM childtable_temp WHERE foreignkey = $PID)");  
$db->close();  

Not sure whether my syntax is incorrect or whether it is something to do with the fact that multiple rows are selected in the subquery mixed with a single value for the $LastID, which causes an error.

Can anyone help? Thanks!
May be there is an easier way of doing this
_________________
Andy

RAD tools for rich UI controls:
http://www.koolphptools.com
View profile  Send private message
mentecky

Posts: 321
Posted: 09/25/2008, 8:22 AM

andy,

Try:
INSERT INTO childtable (foreignkey, fieldA, fieldB, fieldC)     
SELECT $LastID, fieldA, fieldB, fieldC FROM childtable_temp WHERE foreignkey = $PID;

I think that's right.

Rick
_________________
http://www.ccselite.com
View profile  Send private message
andy


Posts: 183
Posted: 09/25/2008, 9:08 AM

Rick

You're the man!

Spot on. Thanks a bundle. Yee ha!
_________________
Andy

RAD tools for rich UI controls:
http://www.koolphptools.com
View profile  Send private message
mentecky

Posts: 321
Posted: 09/25/2008, 8:38 PM

Andy

No problem! Glad to help!

Rick

_________________
http://www.ccselite.com
View profile  Send private message
syah

Posts: 29
Posted: 04/14/2009, 3:01 AM

where u put this coding?
is that before build insert event...
i just want to know to solve my problem...
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.