PHPHEHE
PHPHEHE

Reputation: 11

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 normally 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

Upvotes: 1

Views: 117

Answers (1)

AJ.
AJ.

Reputation: 28174

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.

Upvotes: 1

Related Questions