Php 无法循环通过每个
我有一个表单,用户可以在其中更新配方。如果他们希望编辑的配方是系统默认的配方,则会添加新配方,并从他们的食谱中删除原始配方的副本 如果他们正在更新的配方是他们个人的配方,则需要执行简单的更新查询 我可以计算出这两种情况的逻辑,但对于这两种情况,我不能循环使用配料和数量数组进行更新。保存时显示为空。我不确定表单或for-each循环是否有问题 表单中的代码片段包括:Php 无法循环通过每个,php,html,sql,Php,Html,Sql,我有一个表单,用户可以在其中更新配方。如果他们希望编辑的配方是系统默认的配方,则会添加新配方,并从他们的食谱中删除原始配方的副本 如果他们正在更新的配方是他们个人的配方,则需要执行简单的更新查询 我可以计算出这两种情况的逻辑,但对于这两种情况,我不能循环使用配料和数量数组进行更新。保存时显示为空。我不确定表单或for-each循环是否有问题 表单中的代码片段包括: <form enctype="multipart/form-data" action="editDisplay.php" me
<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);
}
}
}
?>
if(isset($\u POST['submit'])){
$recipeID=$_POST['id'];
$name=$_POST['name'];
$description=$_POST['description'];
$method=$_POST['method'];
$duration=$_POST['duration'];
$defestion=$_POST['defestion'];
$source=$_POST['source'];
$配料=$_POST[“配料”];
$quantity=$_POST['quantity'];
$id=$_POST['ingredId'];
$comment=$_POST['comment'];
$date=$_POST['date'];
$image=$_POST['image'];
$defaultRecipe=$_POST['defaultRecipe'];
$timestamp=日期(“Y/m/d”);
$userRate='1';
如果($name!=''&&$description!=''&&&$method!=''&&&$duration!=''&&&&$consolution!=''){
如果($defaultRecipe==0){
$query1=$db->prepare(“更新配方集名称=:名称,描述=:描述,方法=:方法,持续时间=:持续时间,难度=:难度,源=:源,其中id=:id”);
$dbParams1=array('name'=>$name,'description'=>$description,'method'=>$method,'duration'=>$duration,'demobility'=>$demobility,'source'=>$source,'id'=>$recipeID);
$query1->execute($dbParams1);
foreach($配料作为$配料){
//$component=$value;
//打印(数组值($Components));
//回声“
”;
//echo美元价值;
foreach(数量为$quantity){
回声$数量;
foreach($id作为$value){
echo美元价值;
echo$id;
打印(价值);
$query2=$db->prepare(“更新配料集配料=:配料,数量=:数量,其中id=:id”);
$dbParams2=array('components'=>$components,'quantity'=>$quantity,'id'=>$value);
$query2->execute($dbParams2);
}
}
}
$query4=$db->prepare(“更新用户_烹饪书集注释=:注释,注释日期='$timestamp',其中recipeID=:recipeID和userID=:userID”);
$dbParams4=array('comment'=>$comment,'recipeID'=>$recipeID,'userID'=>$thisUser);
$query4->execute($dbParams4);
}
否则{
$default=0;
$query10=$db->prepare(“插入到配方值中(NULL,:name,:description,:method,:duration,:难点,:source,:userRate,:defaultRecipe)”;
$dbParams10=array('name'=>$name,'description'=>$description,'method'=>$method,'duration'=>$duration,'defessure'=>$source,'userRate'=>$userRate,'defaultRecipe'=>$default);
$query10->execute($dbParams10);
$recipeID1=$db->lastInsertId();
$query400=$db->prepare(“插入到用户的食谱值中(NULL,:userID,:recipeID,:comment,$timestamp”);
$dbParams400=array('userID'=>$thisUser,'recipeID'=>$recipeID1,'comment'=>$comment);
$query400->execute($dbParams400);
foreach($成分作为$成分){
foreach($数量作为$数量){
$query2=$db->prepare(“插入配料值(NULL,:配料,:数量)”);
$dbParams2=array('components'=>$component,'quantity'=>$quantilities);
$query2->execute($dbParams2);
$ingredientID=$db->lastInsertId();
$query3=$db->prepare(“插入配方中的配料值(NULL,:recipeID,:ingredientID)”;
$dbParams3=array('recipeID'=>$recipeID,'ingredientID'=>$ingredientID);
$query3->execute($dbParams3);
}
}
$query500=$db->prepare(“从用户_食谱中删除,其中recipeID=:recipeID和userID=:userID”);
$dbParams500=数组('recipeID'=>$recipeID,'userID'=>$thisUser);
$query500->execute($dbParams500);
}
}
}
?>
如果您不知道问题出在哪里,请先找出问题所在。这是相当简单的调试。如果我错了,请纠正我,但我99%确定foreach上不能有相同的变量名。如果将$quantity作为$quantity,请尝试使用$quantity作为$quantity并根据True Lara重命名。在foreach语句中使用相同的名称可以有效地破坏内容。在if语句的foreach中更改名称有效,但在else中没有发送数据