PHP/MYSQL/Keys:将表一致地插入到两个表中
我已经在 phpmyadmin 中创建了这些表,
food -->(id, user_id, name, price, description)
ingredient-->(id, name)
food_ingredient--> (id, food_id, ingredient_name)
我希望 food_id
与 food.id 匹配,与 ingredient_name
和 ingredient.name
无论如何,我想插入数据当用户提供 food.name
、food.price
、时,同时输入
信息。food
和 food_ingredient
>食物.描述
所以我(在对用户输入进行了一些剥离和修剪之后):
INSERT INTO food
(id, user_id, name, price, description)
VALUES
(NULL,\"$user_id\", \"$name\",\"$price\",\"$food_desc\")"
问题是如何获取 food_id
以便我可以运行如下所示的内容:
INSERT INTO food_ingredient
(id, food_id, ingredient_name)
VALUES
(NULL,\"$food_id\", \"$ingredient_name\")
I have created these tables in phpmyadmin
food -->(id, user_id, name, price, description)
ingredient-->(id, name)
food_ingredient--> (id, food_id, ingredient_name)
I want food_id
to match with food.id same with ingredient_name
and ingredient.name
Anyways I want to insert data into food
and food_ingredient
at the same time when a user provides food.name
, food.price
, food.description
information.
So I have (after doing some stripping and trimming of user input):
INSERT INTO food
(id, user_id, name, price, description)
VALUES
(NULL,\"$user_id\", \"$name\",\"$price\",\"$food_desc\")"
The question is how do I get food_id
so that I can run something like:
INSERT INTO food_ingredient
(id, food_id, ingredient_name)
VALUES
(NULL,\"$food_id\", \"$ingredient_name\")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个 INSERT 之后使用
mysql_insert_id
函数 并将值存储在可在相关插入中使用的变量中。Use the
mysql_insert_id
function after the the firstINSERT
and store the value in a variable that you can use in the related inserts.