user3347558
user3347558

Reputation: 1

Foreign key and primary key

I'm making a page in dreamweaver using phpmyadmin. I want to register names of people attending an event or a show.

My question is: if I have a form in dreamweaver which posts name into one table and automatically gives the person an id (person_id) by auto increment, how can I make the foreign key automatically post the same id? And also, if I register a person with a same name that's already registered, how can I make it have the same person_id?

Thanks in advance :)

Upvotes: 0

Views: 126

Answers (1)

Barmar
Barmar

Reputation: 781320

After inserting a row, the LAST_INSERT_ID() MySQL function returns the auto-increment ID that was assigned to it. All the MySQL APIs provide a way to get this, e.g. PDO's lastInsertId() method. You can use this when updating the table with the foreign key.

If a person with the same name should get the same ID, you can make the name a unique key. An attempt to register the same name will result in an error, and you can then perform a query to get their ID.

Upvotes: 2

Related Questions