PDO在foreach循环中更新行值

时间:2016-09-25 22:34:38

标签: php pdo foreach

我在foreach循环中创建了一个表,在表中显示所有记录。然后我包含了一个带有select和update一个数组值的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>';

            }



        ?>

谢谢。

1 个答案:

答案 0 :(得分: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)); 
                }
            }