【问题标题】:PDO update row value in foreach loopPDO 在 foreach 循环中更新行值
【发布时间】:2017-02-03 04:25:23
【问题描述】:

我创建了一个表格,在一个 foreach 循环中显示表格中的所有记录。然后我在选择中包含了一个 SQL 更新并更新了一个数组值。当我选择不同的值时,循环会更新表中的所有记录,而不仅仅是选择要更新的记录。我整天都在为此苦苦挣扎,请帮助我。

如果 foreach 是错误的方式,我非常感谢您对如何显示可以显示记录并允许更新每个数组的表的建议。

<?php
            foreach ($stmt as $key => $row)
            {

                $newcustomerid = htmlentities($row['customer_id']);

                echo '<tr><td>' . htmlentities($row['customer_company']) . '</td>';
                echo '<td> id:' . $newcustomerid . 'key: ' . $key .'</td>';
                echo '<td>' . htmlentities($row['customer_email']) . '</td>';
                echo '<td>' . htmlentities($row['customer_phone']) . '</td>';
                echo '<td>' . htmlentities($row['customer_country']) . '</td>';
                echo '<td>' . htmlentities($row['customer_city']) . '</td>';
                echo '<td>' . htmlentities($row['customer_segment']) . '</td>';
                echo '<td>' . htmlentities($row['customer_updated']) . '</td>';
                $customer_status = htmlentities($row['customer_status']);

                if(isset($_POST['Submitbb'])){ //check if form was submitted
                $input = $_POST['Submitbb']; //get input text

                    $stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
                    $stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $row['customer_id']));
                }

                echo '<td>
                    <form action="" method="post">
                        <select name="Submitbb" onchange="this.form.submit();">
                            <option> - ' . $customer_status . ' - </option>
                            <option>Susisiekti</option>
                            <option>Priminimas 1</option>
                            <option>Priminimas 2</option>
                            <option>Paskambinti</option>
                            <option>Netinkamas klientas</option>
                            </select>
                    </form>
                </td>';


                echo '<td>' . 'Išsaugoti' . '</td></tr>';

            }



        ?>

谢谢。

【问题讨论】:

    标签: php pdo foreach


    【解决方案1】:

    您可以使用隐藏输入来捕获您希望更新的数据库中的行

     echo '<td>
                    <form action="" method="post">
                        <input name="customer_id" type="hidden" value="'.$newcustomerid.'"/>  <!-- hidden -->
                        <select name="Submitbb" onchange="this.form.submit();">
                            <option> - ' . $customer_status . ' - </option>
                            <option>Susisiekti</option>
                            <option>Priminimas 1</option>
                            <option>Priminimas 2</option>
                            <option>Paskambinti</option>
                            <option>Netinkamas klientas</option>
                            </select>
                    </form>
                </td>';
    

    然后在表单发布的时候进行比较

                if(isset($_POST['Submitbb'])){ //check if form was submitted
                $input = $_POST['Submitbb']; //get input text
                $customer_id =  $_POST['customer_id']; // get the posted customer ID
                if($row['customer_id'] == $customer_id){
    
                        $stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
                        $stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $customer_id)); 
                    }
                }
    

    【讨论】:

    • 谢谢!但是..它在您发布的第二个代码块的第三行中显示:“未定义索引:customer_id”..
    • 嘿,为这两个块复制一份新副本,我已经进行了多次编辑,包括修复错误
    • 非常感谢,可惜现在没有提交。我测试了几次,数据库没有更新。 :/ @安德鲁
    • 哦,我想我知道为什么了,我没注意到你用的是单引号,我把它改成了这样 &lt;input name="customer_id" type="hidden" value="'.$newcustomerid.'"/&gt; 看看编辑
    • 没有错误,没有,但是数据没有提交。我将继续对其进行测试并试图找出原因.. :/
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2015-10-29
    • 2019-10-10
    • 2016-09-28
    • 1970-01-01
    相关资源
    最近更新 更多