doucuodan0897 2016-09-25 14: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 14: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部