将数据插入多个表 PHP MySQL

2022-01-21 00:00:00 dataset 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()

相关文章