...

View Full Version : how to add data from a form into a foreign key



juslai
11-05-2012, 05:14 PM
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

Fou-Lu
11-05-2012, 06:05 PM
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.

juslai
11-06-2012, 12:19 AM
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?

Fou-Lu
11-06-2012, 02:48 PM
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.

juslai
11-06-2012, 04:27 PM
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

Fou-Lu
11-06-2012, 04:56 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum