gpt4 book ai didi

php - 配方数据库 - 添加具有现有或额外成分的配方

转载 作者:行者123 更新时间:2023-11-29 06:21:43 25 4
gpt4 key购买 nike

你好,谢谢抽出时间。

我已经建立了一个食谱数据库: Recipe Database ER Diagram .由 4 个表组成;

食谱(recipe_id、recipe_name、recipe_description、recipe_time)

成分(ingredient_id, ingredient_name)

recipe_ingredients (recipe_id, ingredient_id, amount)

说明(recipe_id、步骤、step_description)

我想通过网站让我的用户将食谱添加到食谱表,并将配料添加到配料表。因此,将添加以前未添加到数据库中的新成分,而不会添加那些已经添加的成分(这应该不是问题)。

我遇到的问题是在 recipe_ingredients 表中将这些新旧成分(以及与此相关的说明)链接到我的用户正在添加的食谱。

到目前为止我的代码是 (html):

<form action="insert.php" method="post">
<p>
<label for="RecipeName">Recipe Name</label>
<input type="text" name="recipename" id="recipeName">
</p>
<p>
<label for="IngredientName">Ingredient Name</label>
<input type="text" name="ingredientname" id="ingredientName">
</p>
<input type="submit" value="Add Records">

还有我的 php:

$sql = "INSERT INTO ingredients (ingredient_name) VALUES ('$ingredient_name');";
$sql .= "INSERT INTO recipes (recipe_name) VALUES ('$recipe_name')";
if ($link->multi_query($sql) === TRUE) {
echo "New records created successfully";}
else {
echo "Error: " . $sql . "<br>" . $link->error;
}

请提出任何问题,我欢迎任何能得到的帮助。

最佳答案

我不知道我是否理解你所有的问题,也不知道 SO 是否是解决问题的最佳方式,但这里有一些提示,希望可以帮助你找到解决方案:

添加新食谱的食谱:

  • 首先,添加配方(新的或修改过的)

  • 其次,将配料依次添加到新配方中。

详细信息:

首先,根据提供的模型,在你的表单中添加一个amount字段:

<form action="insert.php" method="post">
<p>
<label for="RecipeName">Recipe Name</label>
<input type="text" name="recipename" id="recipeName">
</p>
<p>
<label for="IngredientName">Ingredient Name</label>
<input type="text" name="ingredientname" id="ingredientName">
</p>
<p>
<label for="amountOf">Amount</label>
<input type="number" min="0" max="99" name="amount" id="amountOf">
</p>
<input type="submit" value="Add Records">

提交后,您需要第一部分按名称查找重复的食谱,并定义它是新食谱还是现有食谱。像 SELECT FROM recipes WHERE recipe_name LIKE '%$recipename%' 这样的查询可以解决这个问题(我认为你的食谱名称是唯一的。最好的方法是获取你的食谱 ID形式,而不是名称)。

现在,如果它是一个新食谱,您可以这样做(假设您使用的是 Mysqli ):

$sql = "INSERT INTO recipes (recipe_name) VALUES ('$recipe_name');"
$sql. = "INSERT INTO ingredients (ingredient_name) VALUES
('$ingredient_name')";
if ($link->multi_query($sql) === TRUE) {
$recipeId = $link->insert_id;
$link->next_result();
$ingredientId = $link->insert_id;
$sql2 = "INSERT INTO recipe_ingredients(recipe_id,ingredient_id,amount) VALUES ($recipeId,$ingredientId,$amount);"
if ($link->query($sql2) == TRUE){
echo "New records created successfully";}
}
else {
echo "Error: " . $sql2 . "<br>" . $link->error;
}
}
else {
echo "Error: " . $sql . "<br>" . $link->error;
}

如果菜谱不是新的,你可以试试:

$sql = "INSERT INTO ingredients (ingredient_name) VALUES 
('$ingredient_name')";
if ($link->query($sql) === TRUE) {
//retrive your $recipeId from the SELECT query
$ingredientId = $link->insert_id;
$sql2 = "INSERT INTO recipe_ingredients(recipe_id,ingredient_id,amount) VALUES ($recipeId,$ingredientId,$amount);"
if ($link->query($sql2) == TRUE){
echo "New records created successfully";}
}
else {
echo "Error: " . $sql2 . "<br>" . $link->error;
}
else {
echo "Error: " . $sql . "<br>" . $link->error;
}

这些代码只是示例,您需要处理可能的 errorsexceptions .看看try/catch/finally block 来处理异常。

Here is关于使用 mysqli->next_result() 来帮助您处理需要检索的不同 ID 的 SO 问题。

更新

正如您在评论中所问并遵循您的模型,要将各种成分插入同一食谱中,您需要将表格分成两种不同的形式,一种用于插入食谱,另一种用于将成分插入特定的(和预-存在)配方。

然后,重复查询:

INSERT INTO ingredients (ingredient_name) VALUES ('$ingredient_name')

INSERT INTO recipe_ingredients(recipe_id, ingredient_id, amount) 
VALUES ($recipeId, $ingredientId, $amount)

对于每个插入的成分。

如果可以的话,只是一个建议。显然,您在理解模型及其在真实关系数据库中的工作方式时遇到了一些麻烦。

也许关于这个主题的一些教程对您来说是件好事。

尝试一些:

我对这些教程一无所知(现在只是用谷歌搜索),但这将是一个很好的开始,可以帮助您更好地了解数据库的工作原理以及每项任务需要哪些查询。

关于php - 配方数据库 - 添加具有现有或额外成分的配方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com