php - 无法循环遍历每个
问题描述
我有一个表格,用户可以在其中更新食谱。如果他们希望编辑的菜谱是系统默认的菜谱,则使用添加的新菜谱创建新菜谱,并从他们的菜谱中删除原始菜谱的副本。
如果他们正在更新的菜谱是他们个人的菜谱,则将进行简单的更新查询。
我可以计算出这方面的逻辑,但对于这两种情况,我无法循环通过成分和数量数组进行更新。保存它显示为空。我不确定表单或 for each 循环是否有问题。
表单中的代码片段包括:
<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);
}
}
}
?>
解决方案
推荐阅读
- content-management-system - 寻找订阅数据库项目的建议和建议
- python - 二元矩阵的最小方差
- linux - 安装内核模块时出现未知符号错误
- wordpress - 如何仅通过 url 显示访问者的姓名?
- java - 如何从两个不同的活动中访问 Firebase 实时数据库?
- apache-kafka - Kafka Consumer 完全控制分区和偏移量?
- cmd - Runas / Savecred CMD 提示未键入
- javascript - Three.js - 如何改变透视相机的弯曲/曲线?
- python - 如何在 psycopg2 线程连接类中重新连接到 postgreSQL?SSL SYSCALL 错误导致的失败:在 Azure 中检测到 EOF?
- javascript - 获取实例上的方法