Inserting data into multiple tables PHP MySQL(将数据插入多个表 PHP MySQL)
问题描述
我有一个用于存储食谱的基本数据结构.它由以下三个表组成:
I have a basic data structure for storing recipes. It consists of three tables as below:
表 1 - 食谱(recipe_id、recipe_name)
表 2 - 成分(ingredient_id、成分名称)
表 3 - Recipe_Ingredients(recipe_id、component_id)
我在添加新配方时遇到了问题,想知道插入的最佳做法.
I have come across a problem when adding a new recipe and would like to know the best practice for inserting.
目前,在提交我插入到 Recipes 表中的表单时,recipe_id 是自动生成的.然后我插入到 Ingredients 表中,再次自动生成成分 ID.提交的第三步是然后插入到 Recipe_Ingredients 表中,但是我如何获取刚刚创建的食谱和成分 ID 的值以便将它们插入到 Recipe_Ingredients 桌子?
Currently, on submitting the form I insert into the Recipes table, the recipe_id is auto generated. I then insert into the Ingredients table, again the ingredient_id is auto generated. The third step on submit is to then insert into the Recipe_Ingredients table, but how do I get the values of the recipe and ingredient ids that have just been created in order to insert them into the Recipe_Ingredients table?
我目前有单独的 PHP 函数可以插入到 Recipes 和 Ingredients 表中.
I currently have separate PHP functions to insert into the Recipes and Ingredients tables.
推荐答案
每次插入操作后都要获取插入的id值.
You have to get the inserted id value after each insert operation.
获取最后插入id的函数是:mysql_insert_id()
The function to get the last insert id is: mysql_insert_id()
这篇关于将数据插入多个表 PHP MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将数据插入多个表 PHP MySQL


- SoapClient 设置自定义 HTTP Header 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- Laravel 仓库 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01