将数据插入多个表PHP MySQL [英] Inserting data into multiple tables PHP MySQL

查看:427
本文介绍了将数据插入多个表PHP MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于存储食谱的基本数据结构。它由三个表组成:



表1 - 配方(recipe_id,recipe_name)


$ b $表2 - 成分(份数,成分名称)



表3 - Recipe_Ingredients(recipe_id,ingredients_id)



添加新食谱时遇到问题,想知道插入的最佳做法。



目前,在将表单插入配方表格时,会自动生成recipe_id。然后,我将插入到成分表中,然后将会自动生成ingredients_id。提交的第三步是插入到 Recipe_Ingredients 表中,但是如何获取刚刚创建的配方和成分ID的值,以便将它们插入到 Recipe_Ingredients



我目前有单独的PHP函数插入到配方成分表中。

解决方案

每次插入操作后,您必须得到插入的id值。



获取最后一个插入ID的功能是: mysql_insert_id()


I have a basic data structure for storing recipes. It consists of three tables as below:

Table 1 - Recipes (recipe_id, recipe_name)

Table 2 - Ingredients (ingredient_id, ingredient_name)

Table 3 - Recipe_Ingredients (recipe_id, ingredient_id)

I have come across a problem when adding a new recipe and would like to know the best practice for inserting.

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?

I currently have separate PHP functions to insert into the Recipes and Ingredients tables.

解决方案

You have to get the inserted id value after each insert operation.

The function to get the last insert id is: mysql_insert_id()

这篇关于将数据插入多个表PHP MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆