PHP / MYSQL: Adding records based on foreign keys
I've developed a basic PHP front-end for my MySQL database, and I'd like to know the method for adding a new record based on foreign keys in PHP.
I have two tables, Event and Course. The Event table contains EventID, CourseID, and EventDate. The Course table contains CourseID, and CourseName.
The command would normall开发者_开发百科y be
INSERT INTO event
(EventID,CourseID,EventDate) VALUES ( NULL,'2','2011-03-01');
But how do I do this in PHP by providing CourseName instead of CourseID
You should store the CourseID as a hidden form field in your HTML/PHP form. When the user is submitting an Event for the Course, the form will include a value for the CourseID in the values sent to your PHP form action. This way, you don't have to look the CourseID up based on some (possibly non-unique) CourseName.
精彩评论