- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好,谢谢抽出时间。
我已经建立了一个食谱数据库: 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;
}
这些代码只是示例,您需要处理可能的 errors和 exceptions .看看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/
我有一些带有反应组件抽象的外部 UI,我想从试剂中重用它们,有没有什么方法可以通过从 clojurescript 传递数据来直接渲染预定义的 react 组件。我是 clojurescript 初学者
我刚刚构建了一个库(Material Components库)from source并将本地Maven存储库添加到了我的项目中。现在,我可以通过将Log调用添加到库的本地源中来成功地进行一些外行调试。
我正在尝试测试呈现 grommet 的组件菜单 组件。索环 Menu 组件将绝对定位的菜单呈现到文档的顶层,作为子级插入到 body 中。因此它呈现在包装器的范围之外。我可以使用 document.b
如何创建一个凹形的 SKPhysicsBody? 我的猜测是创建一个由多个凸体组成的复合节点。我可以用任何其他方式“粘贴”它们,从而在它们之间创建 SKPhysicsJointFixed 吗? 最佳答
我正在开发一个食谱应用程序来帮助我妻子培养她的蛋糕爱好。这个想法是创建一个食谱数据库来保存她所有的蛋糕食谱。 每个食谱都有多种成分。每种成分都会有测量值(克、毫升、茶匙等),然后是数量。 我了解如何创
我正在使用 sklearn's PCA用于对大量图像进行降维。安装 PCA 后,我想看看组件的外观。 可以通过查看 components_ 属性来做到这一点。没有意识到这是可用的,我做了其他事情: e
我是一名优秀的程序员,十分优秀!