Php 配方数据库-添加含有现有或其他成分的配方

Php 配方数据库-添加含有现有或其他成分的配方,php,mysql,Php,Mysql,您好,谢谢您的时间 我已经建立了一个配方数据库: . 由4个表组成 配方(配方id、配方名称、配方描述、配方时间) 成分(成分id、成分名称) recipe\u配料(配方id、配料id、数量) 说明(配方id、步骤、步骤说明) 我想通过一个网站,让我的用户将配方添加到配方表,并将配料添加到配料表。这样,以前没有添加到数据库中的新成分将被添加,而那些已经添加的成分将不会被添加(这应该已经不是问题) 我遇到的问题是将这些新的和旧的配料(以及相关说明)链接到用户在配方配料表中添加的配方 到目前为止,我

您好,谢谢您的时间

我已经建立了一个配方数据库: . 由4个表组成

配方(配方id、配方名称、配方描述、配方时间)

成分(成分id、成分名称)

recipe\u配料(配方id、配料id、数量)

说明(配方id、步骤、步骤说明)

我想通过一个网站,让我的用户将配方添加到配方表,并将配料添加到配料表。这样,以前没有添加到数据库中的新成分将被添加,而那些已经添加的成分将不会被添加(这应该已经不是问题)

我遇到的问题是将这些新的和旧的配料(以及相关说明)链接到用户在配方配料表中添加的配方

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



配方名

成分名称

还有我的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;
}
$sql=“插入配料(配料名称)值(“$配料名称”);”;
$sql.=“插入配方(配方名称)值(“$recipe\u name”)”;
如果($link->multi_query($sql)==TRUE){
回显“已成功创建新记录”;}
否则{
echo“Error:”.$sql.“
”$link->Error; }

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

我不知道我是否理解您的所有问题,也不知道这样做是否是解决问题的最佳方式,但以下是一些建议,我希望可以帮助您找到解决方案:

添加新配方的配方:

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

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

详情:

首先,根据提供的模型,在表单中输入一个
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">
如果配方不是新的,您可以尝试:

$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;
}

对于插入的每种成分

如果可以的话,只是一个建议。显然,您在理解您的模型以及它如何在真正的关系数据库中工作时遇到了一些困难

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

试试:


我对这些教程一无所知(现在就用谷歌搜索一下),但这将是更好地了解数据库工作方式以及每个任务需要哪些查询的良好开端。

您是否收到了一些错误消息?不,我没有收到任何错误消息,到目前为止,代码工作正常。我遇到的问题是,我想将用户添加到配方中的特定配料链接到配方配料表中的配方。很遗憾,我不知道怎么做。你的问题是缺乏对多对多关系的理解吗?在添加条目之前,您只需简单地输入配方和配料的id。该功能非常好,非常感谢您的时间和代码。我想,如果能够在同一份食谱中添加多种成分,而不是仅仅添加一种成分,那么如何才能做到这一点呢?再次感谢你的帮助!
$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;
}
INSERT INTO ingredients (ingredient_name) VALUES ('$ingredient_name')
INSERT INTO recipe_ingredients(recipe_id, ingredient_id, amount) 
                       VALUES ($recipeId, $ingredientId, $amount)