Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2012, 04:14 PM   PM User | #1
juslai
New Coder

 
Join Date: Jul 2012
Location: Philippines
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
juslai is an unknown quantity at this point
how to add data from a form into a foreign key

basically i have first form that ask for a plate number then the data from the plate number table will be put into a drop down menu for another form but the other table has a foreign key from the plate number table i want to be able to insert data to driver table but it wont let me cause it says cannot add or update a child row how to do it so that the foreign key will get data from the drop down menu created by the other table the data from the drop down menu is for the foreign key
juslai is offline   Reply With Quote
Old 11-05-2012, 05:05 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I don't understand the question. If you have a record already, simply take that primary key and provide it to the form so it can be used by the child table during process. If you do not yet have a record, insert into the parent table first, then use LAST_INSERT_ID() to get the insert id of the parent and use it in the child table. This only works on auto_increment primary keys since otherwise you already know what the primary key is.
Fou-Lu is offline   Reply With Quote
Old 11-05-2012, 11:19 PM   PM User | #3
juslai
New Coder

 
Join Date: Jul 2012
Location: Philippines
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
juslai is an unknown quantity at this point
what i mean is this table that im adding wont allow me to add data on it unless i add data to the foreign key but i dont know how to add data to a foreign key what do i need to do join the data from the other table into the foreign key?
juslai is offline   Reply With Quote
Old 11-06-2012, 01:48 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
What are you passing through your form to identify the parent table? That should be either the primary key directly, or an alternate unique identifier that can be used to obtain the primary key. Use that to gather whatever information you need before you insert into the child table.
Fou-Lu is offline   Reply With Quote
Old 11-06-2012, 03:27 PM   PM User | #5
juslai
New Coder

 
Join Date: Jul 2012
Location: Philippines
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
juslai is an unknown quantity at this point
i already fixed it turns out i just need to insert the value from the primary key from the other table to the foreign key in the table that i needed
juslai is offline   Reply With Quote
Old 11-06-2012, 03:56 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by juslai View Post
i already fixed it turns out i just need to insert the value from the primary key from the other table to the foreign key in the table that i needed
Yes, that's what I said in my first reply. This is what a foreign key relation is all about.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
juslai (11-06-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:34 PM.


Advertisement
Log in to turn off these ads.