[英]how to use the primary key of a table in database as a foreign key in table 2 while inserting the records from a single form simultaneously in php
I am fairly new to web development and currently working on a website using an MVC framework that can capture maintenance work conducted.我是 web 开发的新手,目前在一个使用 MVC 框架的网站上工作,该框架可以捕获进行的维护工作。 I have managed to make the forms and it correctly displays any errors in filling the form and if there aren't any errors successfully inserts it into the database.
我设法制作了 forms,它正确显示了填写表格时的任何错误,如果没有任何错误,则成功将其插入数据库。 What I would like to achieve is having the main table with the general details of the maintenance such as (date, time, technician, department, location, recommendations) and another table for which records what tasks were done during the maintenance such as sweeping, mopping, wiping the windows, cutting grass, etc. I have a single form that requires all the details required in both the tables to be filled.
我想要实现的是让主表包含维护的一般详细信息,例如(日期、时间、技术人员、部门、位置、建议)和另一个记录维护期间完成的任务的表,例如清扫、拖地、擦 windows、割草等。我有一个表格,需要填写两个表格中所需的所有详细信息。 both tables will have primary keys that will be auto-increment.
两个表都将具有自动递增的主键。 I would then like to simultaneously insert the data into the relevant tables only while inserting data into the tasks table I would like to have a foreign key to the main table for that particular record so it corresponds accordingly.
然后我想同时将数据插入相关表,同时将数据插入任务表我想为该特定记录有一个主表的外键,以便它相应地对应。 How can I achieve this without manual input by the user if the primary key of the main table is an auto increment?
如果主表的主键是自增的,那么在没有用户手动输入的情况下如何实现呢?
This isn't a big problem.这不是什么大问题。 It can't be done as a single query, but using transactions you can achieve an all-or-nothing result.
它不能作为单个查询来完成,但使用事务可以实现全有或全无的结果。
In pseudocode:在伪代码中:
The exact mechanics vary between MySQLi and PDO, but the principle is the same.确切的机制在 MySQLi 和 PDO 之间有所不同,但原理是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.