PHP / MYSQL:根据外键添加记录
我已经为我的 MySQL 数据库开发了一个基本的 PHP 前端,我想知道在 PHP 中基于外键添加新记录的方法。
我有两个表,“事件”和“课程”。事件表包含事件 ID、课程 ID 和事件日期。 Course 表包含 CourseID 和 CourseName。
该命令通常是
INSERT INTO event
(EventID,CourseID,EventDate) VALUES ( NULL,'2','2011-03-01');
但是如何在 PHP 中通过提供 CourseName 而不是 CourseID 来做到这一点
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将 CourseID 作为隐藏表单字段存储在 HTML/PHP 表单中。当用户提交课程事件时,表单将在发送到 PHP 表单操作的值中包含 CourseID 的值。这样,您就不必根据某些(可能是非唯一的)CourseName 查找 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.