Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 28th, 2006, 06:38 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting data in multiple tables

Hello !

I have got following tables:

tbZip
(
Zip [primary key],
City ,
StateId [Foreign key],
CountryId [Foreign Key]
)

tbState
(
StateId [pk],
StateNm,
CountryId [fk]
)

tbCountry
(
CountryId [pk],
CountryNm ,
Continent
)

If I want to insert data in tbZip, I am using either of the statements:

Insert into tbZip(Zip, City, StateId, CountryId)

Values ('12345', 'xyz', 1,2)

--OR--

Insert into tbZip(Zip, City, StateId, CountryId)
select '12345' as Zip, 'xyz' as City, 1 as StateId, 2 as CountryId

Both of these are working.

But for this.. each time I have to look for values of StateId and CountryId in the respective tables.

Can it be possible that.. I can write a single Insert statement to insert values directly from tbState and tbCountry tables.. Something like:

Insert into tbZip(Zip, City, StateId, CountryId)

Values ('12345', 'xyz', (select StateId from tbState where StateNm='MH'), select Country Id from tbCountry where CountryNm='IN'))

The purpose is.. if the values of StateId and CountryId are dependent upon tbState and tbCountry then..the values should validate from the respective tables for specific user selected values.

Please Help.
Thanks



Reply With Quote
  #2 (permalink)  
Old August 29th, 2006, 11:32 PM
Registered User
 
Join Date: Aug 2006
Location: Yucaipa, CA, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wrong forum, but here goes:

This will not write any rows if your state or country is not in the related tables:

Code:
INSERT INTO tbZip(Zip, City, StateId, CountryId)
SELECT '12345', 'xyz', tbState.StateId, tbCountry.CountryId
FROM tbState, tbCountry 
WHERE StateNm='MH'
AND CountryNm='IN'
If your referential integrity rules allow nulls then:

Code:
INSERT INTO tbZip(Zip, City, StateId, CountryId)
SELECT '12345', 'xyz', 
(SELECT StateId FROM tbState WHERE StateNm='MH'),
(SELECT CountryId FROM tbCountry WHERE CountryNm='IN')
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting Data into Multiple Tables bfoley SQL Server ASP 1 January 3rd, 2008 12:28 AM
Inserting a record and contents in multiple tables Shell84 ASP.NET 2.0 Professional 6 August 15th, 2006 05:31 PM
data from multiple tables keyvanjan Classic ASP Basics 1 January 24th, 2006 06:32 PM
Inserting data into 2 tables chrscote Access ASP 1 August 1st, 2005 09:01 AM
inserting into multiple tables cridley SQL Server 2000 1 January 25th, 2005 12:05 PM



All times are GMT -4. The time now is 07:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.