简体   繁体   English

PHP/MYSQL/Keys:将表一致地插入到两个表中

[英]PHP/MYSQL/Keys: Inserting Table into Two Tables Consistently

I have created these tables in phpmyadmin我在 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我希望food_id与 food.id 匹配,与ingredient_name名称和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.无论如何,当用户提供food.namefood.pricefood.description信息时,我想同时将数据插入到foodfood_ingredient中。

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:问题是我如何获得food_id以便我可以运行类似的东西:

INSERT INTO food_ingredient

(id, food_id, ingredient_name)

VALUES 

(NULL,\"$food_id\", \"$ingredient_name\")

Use the mysql_insert_id function after the the first INSERT and store the value in a variable that you can use in the related inserts.在第一个INSERT之后使用mysql_insert_id function 并将值存储在可以在相关插入中使用的变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM