I am building a recipe website, where recipes are stored in one table, ingredients in another, and then a third table linking the two with the amount and unit of the ingredient. ie. Recipe table - Recipe ID, Name, Description etc etc
Ingredient Table - IngredientID, Name, Description etc etc
Recipe Ingredients Table- RecipeIngredient ID, Recipe ID, Ingredient ID, Quantity, Unit
So for one recipe, there can me multiple ingredients. To display recipes, I query the recipe_ingredient table for the matching recipeID, and then get the corresponding ingredientID from this and query the ingredients table for each one:
<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 "
";
}
?>
</table>
where I have the following functions
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;
}
The problem is I am having trouble creating a form that will update the ingredients quantities or name.
I currently have this but it does not work:
function update_ingredients($Ingredient_ID, $id, $RecipeIngredientID, $Ingredient, $Quantity, $Unit, $Comment) {
$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());
}
and
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;
}
can anyone help!
Try following code instead current:
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 ...
- for INSERT, UPDATE, DELETE, DROP mysql_query
returns TRUE on success and FALSE on error. Not name or something else, boolean value only!
$Ingredient_ID in query must have same name as function argument, match all variables.