出于某种原因,当我运行我的SQL查询时,它为Last ID返回0。我的插入查询不起作用,但它在运行时没有给我任何错误。我检查数据库,没有插入或添加任何新内容。奇怪的是,这个查询实际上在另一个页面上工作,我在这里逐字复制,但它不起作用或插入。我很难过这个。我无法弄清楚为什么当我运行此查询时它不会执行插入操作。
require 'database.php';
$ingredient = (string)$_POST['ingredient'];
$meal = (string)$_POST['meal'];
if(isset($ingredient) && ($meal)){
$ingr_exploded = explode(" @", $ingredient);
$ingr = $ingr_exploded[0];
$store = $ingr_exploded[1];
// copy ingredients columns into meal_ingredients table.
$sql = "INSERT INTO meal_ingredients (ingredients_id_fk, quanity, package, cost, store)
SELECT ingredient_id, quanity, package, cost, store
FROM ingredients
WHERE item='$ingr' AND store='$store'";
if ($conn->query($sql) === TRUE) {$last_id = $conn->insert_id;
echo "Last ID = $last_id</br>";}
else {echo "Error: " . $sql . "<br>" . $conn->error;}
// insert meal_name & ingredient_name into table.
$sql2 = "UPDATE meal_ingredients
SET meal_name='$meal', item='$ingr'
WHERE meal_ingredients_id=$last_id";
if ($conn->query($sql2) === TRUE) {echo "Meal Name - Ingredient Set. </br>";}
else {echo "Error: " . $sql . "<br>" . $conn->error;}
}