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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀