php - 更新多维数组sql php

标签 php mysql arrays multidimensional-array

我正在构建一个食谱网站,其中食谱存储在一张表中,配料存储在另一张表中,然后第三张表将两者与配料的数量和单位联系起来。 IE。 配方表 - 配方 ID、名称、描述等

成分表 - IngredientID、名称、描述等

配方成分表- RecipeIngredient ID, Recipe ID, Ingredient ID, Quantity, Unit

所以对于一个食谱,我可以有多种成分。为了显示食谱,我在 recipe_ingredient 表中查询匹配的 recipeID,然后从中获取相应的 ingredientID 并为每个查询查询配料表:

 <table id="ingredients_table">
                        <tr>
                        <th>Ingredient</th>
                        <th>Quantity</th>
                        <th>Unit</th>
                        <th>Comment</th>
                        </tr>
                    <?php 
                        foreach ($ingredient as $value) { 
                            $ingredient_name = get_ingredient_name($value['IngredientID']);
                            echo '<tr>';
                            echo '<td>'.$ingredient_name.'</td>';
                            echo '<td>'.$value['Quantity'].'</td>';
                            echo '<td>'.$value['Unit'].'</td>';
                            echo '<td>'.$value['Comments'].'</td>';
                            echo '</tr>';
                            echo "\n";
                        }
                    ?>
                    </table>

我有以下功能

<pre><code>function get_ingredients($id) { //gets the ingredient ids, quanitites, units and comments $result = mysql_query("SELECT * FROM `recipe ingredients` WHERE RecipeID = $id") or trigger_error(mysql_error()); $array = array(); while($row_ids = mysql_fetch_assoc($result)){ $array[] = $row_ids; } return $array; } function get_ingredient_name($id) { //get an ingredient name given its ID $result = mysql_query("SELECT * FROM `ingredients` WHERE IngredientID = $id") or trigger_error(mysql_error()); $row = mysql_fetch_assoc($result); $ingredient_name = $row['Ingredient']; return $ingredient_name; } </code></pre>

问题是我在创建更新成分数量或名称的表单时遇到问题。

我目前有这个,但它不起作用:

function update_ingredients($Ingredient_ID, $id, $RecipeIngredientID, $Ingredient, $Quantity, $Unit, $Comment) {

<pre><code>$ingredient_name = mysql_query("UPDATE `ingredients` SET Ingredient='$Ingredient' WHERE IngredientID='$IngredientID'") or trigger_error(mysql_error()); $ingredient_details = mysql_query("UPDATE `recipe ingredients` SET Quantity='$Quantity', Unit='$Unit', Comment='$Comment' WHERE RecipeIngredientID='$RecipeIngredientID'") or trigger_error(mysql_error()); } </code></pre> <p>and</p> <pre><code>if (isset($_POST['RecipeName'])) { foreach ($ingredient as $value) { $ingredient_name = get_ingredient_name($value['IngredientID']); $Ingredient = userData($_POST['Ingredient']); $Quantity = userData($_POST['Quantity']); $Unit = userData($_POST['Unit']); $Comment = userData($_POST['Comment']); $update_ingredients = update_ingredients($Ingredient_ID, $id, $RecipeIngredientID, $Ingredient, $Quantity, $Unit, $Comment); echo $update_ingredients; } </code></pre>

谁能帮忙!

最佳答案

尝试使用以下代码代替当前代码:

function update_ingredients($Ingredient_ID, $id, $RecipeIngredientID, $Ingredient, $Quantity, $Unit, $Comment) 
{
    mysql_query("UPDATE `ingredients` SET Ingredient='$Ingredient' WHERE IngredientID='$Ingredient_ID'");
    mysql_query("UPDATE `recipe ingredients` SET Quantity='$Quantity', Unit='$Unit', Comment='$Comment' WHERE RecipeIngredientID='$RecipeIngredientID'");

}

$ingredient_name = mysql_query("UPDATE ... - 对于 INSERT、UPDATE、DELETE、DROP mysql_query 成功时返回 TRUE,错误时返回 FALSE。不是名称或其他内容否则,仅限 bool 值!

查询中的$Ingredient_ID 必须与函数参数同名,匹配所有变量。

关于php - 更新多维数组sql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12420507/

相关文章:

php - ajax 发布数据是否需要 URI 编码?

Android - 连接 MySQL 数据库的最安全方式

php - Yii 偶尔数据库访问丢失

php - 如何将表单数据插入mysql数据库

java - 在两个数组之间使用相同的索引来获取相关信息?

java - 发布值并将图像上传到android中的php服务器

php - 通过 AJAX POST 数据并设置为变量

php - 使用 php 通过 javascript 传递参数

arrays - 将 Nemo.jl 矩阵转换为普通 Julia 数组?

c - C 中的递归数组函数