473,513 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

INSERT ID from foreign key table

I have a form used to populate 2 tables: Provider and Contacts. Provider has
a foriegn key column - Contact1ID - which has a foreign key relationship
with the prmary key - ID - in the Contacts table. The form submits data to
Contacts then Provider but I need the ID generated by the Contacts
submission to be entered into the Contact1ID column in the Provider table.

If I use this:

INSERT INTO Provider(Contact1ID)
SELECT ID FROM Contacts
WHERE FirstName='$contact1{FirstName}'
AND Surname='$contact1{Surname}'

.... all I get is a new row.

UPDATE doesn't seem to allow SELECT to grab the new ID either.

Any ideas?

zaphod
Sep 4 '05 #1
1 2100
zaphod wrote:
I need the ID generated by the Contacts
submission to be entered into the Contact1ID column in the Provider table.


Use the LAST_INSERT_ID() function to get the last ID value generated by
an AUTO_INCREMENT field in your Contacts table. Use that as an
expression in your INSERT INTO Provider statement.

INSERT INTO Contacts (...fields, excluding autoincrement key...) VALUES
(...)
INSERT INTO Provider (Contact1ID, ...) VALUES (LAST_INSERT_ID(), ...)

See http://dev.mysql.com/doc/mysql/en/in...functions.html for
more information on the LAST_INSERT_ID() function in MySQL.

Regards,
Bill K.
Sep 6 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5089
by: Victor M. | last post by:
Hello everyone. I am a newbie to Oracle 8 so please be patient and thanks for your time. The problem is the above mentioned error. I have two databases successully created using SQL*Plus 8.03....
16
16977
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
1
2530
by: nomorems | last post by:
I was trying to copy a range of records from a table to the same table with a small modification. I was wondering why this takes a long time compared to just doing a subselect from that table and...
7
1934
by: trint | last post by:
This: string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " + " TravelDetailUplineId, " + " rankId, " + " TravelDetailId, " + " CreatedDateTime, " + " Operator) " + "VALUES ('" +...
2
12948
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error from the code below! Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
1
12039
by: Wes Groleau | last post by:
INSERT INTO X ...... ( A, B, C ) INSERT INTO Y ...... ( J, K, L ) If Y has a foreign key M which is the primary key D of X, is there an easy and/or efficient way to have SQL Server assign D,...
3
45312
by: weird0 | last post by:
I have two tables accounts and ATM and i am trying to insert a tuple in ATM with accountId as foreign key. But even this simple work,I encounter the following error: The INSERT statement...
7
9755
by: jthep | last post by:
Hi, I'm a newbie at this but how can I populate a table with data from other tables by using INSERT statements? Below is of what I have so far, I have to populate the PERSON_PROFILE table. I've...
2
7323
by: ksenthilbabu | last post by:
Hey All, I am using MSSQL -2005 with VB6. I have created a master table tblCompany and detail Table tblDetail having foreign key relationship. When i try to insert a value within a TRANSACTION I...
5
7541
by: RomeoX | last post by:
Hi everybody actually I need your help in fixing my code. Actually I have a library system that can be applied in university or any school and I'm stucking in a page that for loan student book. I...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.