简体   繁体   English

如何在 php 中同时从单个表单插入记录时将数据库中表的主键用作表 2 中的外键

[英]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:在伪代码中:

  • Validate data验证数据
  • Start a transaction开始交易
  • Insert data into main record插入数据到主记录
  • Get the last inserted ID获取最后插入的 ID
  • Insert one or more records into the child table, using the ID retrieved above使用上面检索到的 ID 向子表中插入一条或多条记录
  • Commit the transaction (or roll back if some error occurred)提交事务(如果发生错误则回滚)

The exact mechanics vary between MySQLi and PDO, but the principle is the same.确切的机制在 MySQLi 和 PDO 之间有所不同,但原理是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在另一个表的主键;外键列中插入记录 - Inserting records in a primary;foreign key columns from another table 来自外键的PHP主键表 - PHP primary key table from foreign key 使用PHP表单将外键数据插入表时遇到问题 - Having a problem inserting a foreign key data into a table using a PHP form 如何从另一个表主键添加mysql外键? - How to Add mysql foreign key from another table primary key? SQL-显示表中的记录,其中外键值与另一个表中的主键值匹配 - SQL - Displaying records from a table where the foreign key value matches the primary key value in another table 如何使用php和mysql将外键字段链接到另一个表的主键字段? - How to link the foreign key field to a primary key field from another table using php and mysql? PHP 在外键表中插入数据 - Inserting data in a foreign key table PHP 使用PHP外键插入mysql表 - Inserting into mysql table using foreign key with PHP Laravel:如何从数据透视表中获取其外键未引用主键的记录? - Laravel : How do I get records from pivot table whereby its foreign key does not reference a primary key? 使用php程序和mysql通过html表单使用主键和外键将数据插入到多个表中 - Insertion of data into multiple table using primary key and foreign key through html form with php procedual and mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM