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.
精彩评论