doucuodan0897 2016-09-25 22:34
浏览 56
已采纳

PDO在foreach循环中更新行值

I created a table, inside a foreach loop displaying in the table all the records. Then I included a SQL update with the select and update one array value. When I select different value, the loop updates all the records in the table and NOT only one selected to be updated. I was all day struggling with it, please help me.

If foreach is a wrong way, I would highly appreciate your suggestions of how to display a table that would display records and permit updates for each array.

<?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>';

            }



        ?>

Thank you.

  • 写回答

1条回答 默认 最新

  • douwayuan3063 2016-09-25 22:46
    关注

    You could use a hidden input to capture the row in the database you wish to update

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

    Then compare it when the form is posted

                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)); 
                    }
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要