drqyxkzbs21968684 2018-05-01 16:07
浏览 24

无法循环每个

I have a form in which a user can update a recipe. If the recipe they wish to edit is a recipe that comes default with the system, a new recipe is created with the additions and the copy of the original recipe is removed from their cookbook.

If the recipe they are updating is a recipe personal to them, a simple update query is to be conducted.

I can work out the logic for this but for both instances, I cannot loop through the ingredients and quantity array to update. Saving it displays as empty. I'm not sure if there is a problem with the form or the for each loops.

Snippets of code from the form include:

<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>

On submission:

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);
    }
}
}

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害