Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    Philippines
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #3
    New Coder
    Join Date
    Jul 2012
    Location
    Philippines
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #5
    New Coder
    Join Date
    Jul 2012
    Location
    Philippines
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    juslai (11-06-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •