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
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
