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


基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01