0

i am getting this error

Food Item Insertion FailedCannot add or update a child row: a foreign key constraint fails (menu_manager.recipe_ingredient, CONSTRAINT recipe_ingredient_ibfk_1 FOREIGN KEY (recipe_id) REFERENCES recipe (recipe_id) ON UPDATE CASCADE)

i am trying to add recipe to my database in one form. and i want the data entered to go to 3 different table. ingredient table recipe table and the recipe_ingredient table. the recipe table has an auto increment id as primary key and the ingredient_id as a foreign key in the recipe table.

i want the ingredient name, data to go to the ingredient table and the name, course, instruction to the recipe table plus the ingredient_id in the recipe table

the the ingredient quantity and unit to go to the ingredient_recipe table which is refferencing the ingredient_id and the recipe_id.

i get the error: Food Item Insertion FailedCannot add or update a child row: a foreign key constraint fails (menu_manager.recipe_ingredient, CONSTRAINT recipe_ingredient_ibfk_1 FOREIGN KEY (recipe_id) REFERENCES recipe (recipe_id) ON UPDATE CASCADE)

thanks in advance for the help guys

  • Post the query you are running as well as your table definitions. – Patrick Q Mar 21 '14 at 13:08
  • possible duplicate of [Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails](http://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa) – Hüseyin BABAL Mar 21 '14 at 13:08
  • You can use the MySQL function LAST_INSERT_ID (or your database adapter's equivalent within your application code) to obtain the recipe ID upon insertion. You can then supply this value when inserting into recipe_ingredient. – Tim Burch Mar 21 '14 at 13:11
  • @ Tim Burch pls can i get a code for that thanks (LAST_INSERT_ID) – user3446323 Mar 21 '14 at 13:21

1 Answers1

0

It's tough to tell exactly what's going on unless you post your code, so it could be a couple of different things. Here's what I can see from what you've posted. Adding an ingredient fails because an ingredient record needs to have the recipe ID of a recipe that is already in the table. You're creating a new recipe, so you probably don't have that recipe ID yet, which is probably where the issue is coming from. You need to add the recipe to the database, commit the change, and then retrieve the auto-generated recipe_id for that record. I think that PHP returns that primary key as a result of a successful commit, but otherwise you'll need to query for it after you add the record to the table. Once you have that recipe_id key, you can include it in the insert statement for the ingredient.

user3446496
  • 361
  • 1
  • 8
  • $sql= "insert into ingredient(ingredient_name) values ('{$ingredient_name}')"; $result = mysql_query($sql); $sql1 = "insert into recipe(ingredient_id,recipe_name,course,instruction ) values('ingredient_id','{$recipe_name}', '{$course}','{$instruction}')"; $result = mysql_query($sql1); $sql1 ="insert into recipe_ingredient(recipe_id,unit,quantity) values('recipe_id','{$quantity}','{$unit}')"; $result = mysql_query($sql1); – user3446323 Mar 21 '14 at 13:27
  • This explains the last_insert_id better than I can. http://us2.php.net/mysql_insert_id – user3446496 Mar 21 '14 at 13:27