Reputation: 825
I need a hand on this...
I want to create a relationship between two tables:
table called films, and a table called directors.
On a form, I've created director_id as a foreign key in the films table, but I don't know how to proceed.
It works like this: I have a form, and it asks me to input a file name, to upload the film, and to input the director name, but the foreign key is director id. In the directors table there is only two columns, director id and director name.
How can I add the director name on the form and INSERT IN the director table having a relation with the director id?
I know I'm explaining it really bad, but it's because I don't know much what am I doing and I'm starting to have a huge mess in my head...
Anyone understands what I am trying to ask? :(
Thank you
Upvotes: 0
Views: 481
Reputation: 616
First execute insert query of director table, 'director_id' field of the directors table should primary key and it should be auto increment.
INSERT INTO directors (name) VALUES ('xyz director')
Then execute following query to get last inserted id from directors table
SELECT LAST_INSERT_ID() as director_id
Now use this last inserted id of directors table as foreign key in the films table
INSERT INTO films (your fields, director_id) VALUES ('your fields', director_id)
Upvotes: 3