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 计算二重积分∫∫e^(x+y)dxdy,其中0≤x≤1,0≤y≤1,试分别用复合辛普森公式(取n=4)以及高斯求积公式(取n=4)计算积分 给出matlab程序
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的