简体   繁体   中英

PHP/MYSQL/Keys: Inserting Table into Two Tables Consistently

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\")

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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