我有一份餐桌,上面列有一份餐食:炸玉米饼,汉堡包,三明治等。然后我有另一张桌子,每餐的成分。肉类,奶酪,生菜,西红柿等。
所以在我的餐桌上,我有很多成分。举个例子,让我们说三明治,这将有成分的总数5.现在我可以手动输入5,但用户正在输入一个未知的成分变量。所以我想说,对于添加的每种成分,膳食中的成分价值都会增加+1。
我将这两个表之间的关系设置为many2many,但我该如何进行算法计算?
这是我到目前为止所做的:
if (isset($_POST['add_ingredient'])){
$meal = (string)$_POST['meal_names'];
$ingr = (string)$_POST['ingredients_list'];
$sql = "INSERT INTO meal_ingredients (quanity, package, cost, store)
SELECT quanity, package, cost, store
FROM ingredients
WHERE item='$ingr'";
if ($conn->query($sql) === TRUE) {
$last_id = $conn->insert_id;
echo "New record created successfully. </br>";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "UPDATE meal_ingredients
SET meal_name='$meal', item='$ingr'
WHERE meal_ingredients_id=$last_id";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully.";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "UPDATE meals
SET ingredients='1'
WHERE meal='$meal'";
if ($conn->query($sql) === TRUE) {
echo "Updated Meal";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}