开发者

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

开发者 https://www.devze.com 2023-03-17 14:11 出处:网络
I have created these tables in phpmyadmin food -->(id, user_id, name, price, description) ingredient-->(id, 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.

开发者_StackOverflow社区

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消