Jump to content

Linking tables with foreign key: design question


kaplanyo

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.