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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.