kaplanyo Posted March 11, 2010 Share Posted March 11, 2010 Hello, I'm using the primary key of one table (work) as a foreign key in another table (workimages), I'm getting how that works. What I'm wondering is if it's necessary to create or insert an entry for the 'work' table, which would then give me a primary key, before I can use that primary key in my other table 'workimages' as a foreign key. In the past I had to create the 'work' entries and then in a separate form use a select drop down menu in order to get the primary key for the 'workimages' upload. I'm trying to create the work entry and at the same time upload images to the workimages in one form. Doesn't seem possible to get the primary key until after it's been inserted. Sorry about my noobness. Any advice would be appreciated. Dave Quote Link to comment https://forums.phpfreaks.com/topic/194901-linking-tables-with-foreign-key-design-question/ Share on other sites More sharing options...
XeNoMoRpH1030 Posted March 11, 2010 Share Posted March 11, 2010 Well, if you are using PHP, you can just do two queries. The first to insert it into the "work" table. You'd then do $insertid = mysql_insert_id(); and use that ID for your "workimages" table. I think you can also use LAST_INSERT_ID() with MySQL, which is exacelt what the PHP command I showed above uses I imagine. It should be pretty reliable as well because it bases it off the connection. Quote Link to comment https://forums.phpfreaks.com/topic/194901-linking-tables-with-foreign-key-design-question/#findComment-1024777 Share on other sites More sharing options...
kaplanyo Posted March 12, 2010 Author Share Posted March 12, 2010 Thanks! I am using PHP and loving it! That did the trick - very much THANKS to you!!! I'm using a variable with that mysql_insert_id(); and passing it in through a function and bam!! right into the ole foreign key... that's so nice! Have a good one! Dave Quote Link to comment https://forums.phpfreaks.com/topic/194901-linking-tables-with-foreign-key-design-question/#findComment-1025200 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.