我有一个表单,用户可以在其中更新配方。如果他们希望编辑的配方是系统默认配方,则会添加一个新配方,并从其食谱中删除原始配方的副本。
如果他们正在更新的食谱是他们个人的食谱,则需要进行简单的更新查询。
我可以为此计算出逻辑但是对于这两个实例,我无法遍历成分和数量数组进行更新。保存它显示为空。我不确定表单或每个循环是否有问题。
表单中的代码片段包括:
<form enctype="multipart/form-data" action="editDisplay.php" method="POST"
enctype="multipart/form-data">
<thead>
<div>
<tr>
<th>
Ingredient
</th>
<th>
Quantity
</th>
</tr>
</thead>
<tbody>
<?php
while ($dbRow2 = $dbQuery3->fetch(PDO::FETCH_ASSOC)) {
$ingredients=$dbRow2["ingredient"];
$quantity=$dbRow2["quantity"];
$id=$dbRow2["id"];
echo '<tr>';
echo '<td>';
?><input type="text" name="quantity[]" value="<?php echo $quantity?>">
<input type="hidden" name="ingredId[]" value="<?php echo $id?>">
<?php
echo '</td>';
echo '<td>';
?><input type="text" name="ingredients[]" value="<?php echo $ingredients
?>">
<?php
echo '</td>';
}
?>
</tbody>
<input class="btn btn-light btn-xl sr-button" name="submit" type="submit" value="Save changes">
</form>
提交时:
if(isset($_POST['submit'])){
$recipeID=$_POST['id'];
$name = $_POST['name'];
$description = $_POST['description'];
$method = $_POST['method'];
$duration = $_POST['duration'];
$difficulty = $_POST['difficulty'];
$source = $_POST['source'];
$ingredients = $_POST['ingredients'];
$quantity = $_POST['quantity'];
$id = $_POST['ingredId'];
$comment = $_POST['comment'];
$date = $_POST['date'];
$image = $_POST['image'];
$defaultRecipe = $_POST['defaultRecipe'];
$timestamp = date("Y/m/d");
$userRate = '1';
if($name !='' && $description !='' && $method !='' && $duration !='' && $difficulty !=''){
if ($defaultRecipe == 0){
$query1 = $db->prepare("update recipe set name=:name, description=:description, method=:method, duration=:duration, difficulty=:difficulty, source=:source where id=:id");
$dbParams1 = array('name'=>$name, 'description'=>$description, 'method'=>$method, 'duration'=>$duration, 'difficulty'=>$difficulty, 'source'=>$source, 'id'=>$recipeID);
$query1->execute($dbParams1);
foreach($ingredients as $ingredients){
//$ingredient = $value;
//print_r(array_values($ingredients));
//echo '<br>';
//echo $value;
foreach ($quantity as $quantity){
echo $quantity;
foreach ($id as $value){
echo $value;
echo $id;
print_r($value);
$query2 = $db->prepare("update ingredients set ingredient=:ingredients, quantity=:quantity where id=:id");
$dbParams2 = array('ingredients'=>$ingredients, 'quantity'=>$quantity, 'id'=>$value);
$query2->execute($dbParams2);
}
}
}
$query4 = $db->prepare("update user_cookbook set comment=:comment, commentDate='$timestamp' where recipeID=:recipeID and userID=:userID");
$dbParams4 = array('comment'=>$comment, 'recipeID'=>$recipeID, 'userID'=>$thisUser);
$query4->execute($dbParams4);
}
else {
$default = 0;
$query10 = $db->prepare("INSERT INTO recipe values (NULL, :name, :description, :method, :duration, :difficulty, :source, :userRate, :defaultRecipe)");
$dbParams10 = array('name'=>$name, 'description'=>$description, 'method'=>$method, 'duration'=>$duration, 'difficulty'=>$difficulty, 'source'=>$source, 'userRate'=>$userRate, 'defaultRecipe'=>$default);
$query10->execute($dbParams10);
$recipeID1 = $db->lastInsertId();
$query400 = $db->prepare("INSERT INTO user_cookbook values (NULL, :userID, :recipeID, :comment, '$timestamp')");
$dbParams400 = array('userID'=>$thisUser, 'recipeID'=>$recipeID1, 'comment'=>$comment);
$query400->execute($dbParams400);
foreach($ingredients as $ingredient){
foreach ($quantity as $quantities){
$query2 = $db->prepare("INSERT INTO ingredients values (NULL, :ingredients, :quantity)");
$dbParams2 = array('ingredients'=>$ingredient, 'quantity'=>$quantities);
$query2->execute($dbParams2);
$ingredientID = $db->lastInsertId();
$query3 = $db->prepare("INSERT INTO recipe_ingredients values (NULL, :recipeID, :ingredientID)");
$dbParams3 = array('recipeID'=>$recipeID, 'ingredientID'=>$ingredientID);
$query3->execute($dbParams3);
}
}
$query500 = $db->prepare("delete from user_cookbook where recipeID=:recipeID and userID=:userID");
$dbParams500 = array('recipeID'=>$recipeID, 'userID' =>$thisUser);
$query500->execute($dbParams500);
}
}
}
?>