开发者

PHP / MYSQL: Adding records based on foreign keys

开发者 https://www.devze.com 2023-03-06 16:10 出处:网络
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'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.

0

精彩评论

暂无评论...
验证码 换一张
取 消