<tfoot id='0AKES'></tfoot>
          <bdo id='0AKES'></bdo><ul id='0AKES'></ul>

        <legend id='0AKES'><style id='0AKES'><dir id='0AKES'><q id='0AKES'></q></dir></style></legend>
      1. <i id='0AKES'><tr id='0AKES'><dt id='0AKES'><q id='0AKES'><span id='0AKES'><b id='0AKES'><form id='0AKES'><ins id='0AKES'></ins><ul id='0AKES'></ul><sub id='0AKES'></sub></form><legend id='0AKES'></legend><bdo id='0AKES'><pre id='0AKES'><center id='0AKES'></center></pre></bdo></b><th id='0AKES'></th></span></q></dt></tr></i><div id='0AKES'><tfoot id='0AKES'></tfoot><dl id='0AKES'><fieldset id='0AKES'></fieldset></dl></div>

        <small id='0AKES'></small><noframes id='0AKES'>

      2. 使用带有 PDO 的数组更新多个 MySQL 表列

        时间:2024-08-22
        • <bdo id='sC4Dt'></bdo><ul id='sC4Dt'></ul>
          1. <i id='sC4Dt'><tr id='sC4Dt'><dt id='sC4Dt'><q id='sC4Dt'><span id='sC4Dt'><b id='sC4Dt'><form id='sC4Dt'><ins id='sC4Dt'></ins><ul id='sC4Dt'></ul><sub id='sC4Dt'></sub></form><legend id='sC4Dt'></legend><bdo id='sC4Dt'><pre id='sC4Dt'><center id='sC4Dt'></center></pre></bdo></b><th id='sC4Dt'></th></span></q></dt></tr></i><div id='sC4Dt'><tfoot id='sC4Dt'></tfoot><dl id='sC4Dt'><fieldset id='sC4Dt'></fieldset></dl></div>
          2. <tfoot id='sC4Dt'></tfoot>

            • <legend id='sC4Dt'><style id='sC4Dt'><dir id='sC4Dt'><q id='sC4Dt'></q></dir></style></legend>
                <tbody id='sC4Dt'></tbody>

                <small id='sC4Dt'></small><noframes id='sC4Dt'>

                1. 本文介绍了使用带有 PDO 的数组更新多个 MySQL 表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将所有 MySQL 连接从旧的 mysql_query 切换到 PDO.我正在尝试使用不同的数组更新 MySQL 表的多行和多列,但收到以下错误:

                  I'm trying to switch all my MySQL connections from the old mysql_query to PDOs. I'm trying to update multiple rows and columns of a MySQL table using different arrays and I'm receiving the following error:

                  [42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法,以便在 '(accnt, car, radio, misc) values ('admin', '300.00', '400.00', '10.00') WHERE ID' at line 附近使用1

                  [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(accnt, car, radio, misc) values ('admin', '300.00', '400.00', '10.00') WHERE ID' at line 1

                  来自以下代码:

                  $account = $_POST['account'];
                  $car_lease = $_POST['car_lease'];
                  $radio_lease = $_POST['radio_lease'];
                  $misc_lease = $_POST['misc_lease'];
                  $lease_ID = $_POST['lease_ID'];
                  
                  //$data = array_map(null,$account,$car_lease,$radio_lease,$misc_lease);
                  $A = count($lease_ID);
                  
                  try {
                      $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
                      $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                      $STH = $DBH->prepare('UPDATE lease (accnt, car, radio, misc) values (:account, :car_lease, :radio_lease, :misc_lease) WHERE ID = :lease_ID');
                      $i = 0;
                      while($i < $A) {
                          $STH->bindParam(':account', $account[$i]);
                          $STH->bindParam(':car_lease', $car_lease[$i]);
                          $STH->bindParam(':radio_lease', $radio_lease[$i]);
                          $STH->bindParam(':misc_lease', $misc_lease[$i]);
                          $STH->bindParam(':lease_ID', $lease_ID[$i]);
                          $STH->execute();
                          $i++;
                      }
                  }
                  catch(PDOException $e) {  
                      echo "I'm sorry, but there was an error updating the database.";  
                      file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
                  }
                  

                  我相信这个问题是由我调用语句句柄的方式引起的,但我不确定我的语法的哪一部分不正确.另外,这是处理这种情况的最佳方法吗?或者有没有更好的方法来更新表中的多行?

                  I believe this problem is arising from the way I'm calling the statement handle, but I'm not sure what part of my syntax is incorrect. Also, is this the best way of handling such situations? Or is there a better method to update multiple rows in a table?

                  推荐答案

                  您混淆了 INSERTUPDATE 语句之间的语法.您需要一个 SET 子句,而不是 VALUES() 列表:

                  You have confused the syntax between INSERT and UPDATE statements. Instead of a VALUES() list, you need a SET clause:

                  $STH = $DBH->prepare('
                      UPDATE lease 
                      SET 
                        accnt = :account, 
                        car = :car_lease, 
                        radio = :radio_lease, 
                        misc = :misc_lease 
                      WHERE ID = :lease_ID
                  ');
                  

                  查看 MySQL UPDATE 语法参考与 UPDATE 语句一起使用的完整规范.

                  Review the MySQL UPDATE syntax reference for the full specification to use with UPDATE statements.

                  这篇关于使用带有 PDO 的数组更新多个 MySQL 表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 PHP 中的时区之间转换 下一篇:如何判断 MySQL UPDATE 何时成功与实际更新的数据?

                  相关文章

                  <legend id='Eh6R6'><style id='Eh6R6'><dir id='Eh6R6'><q id='Eh6R6'></q></dir></style></legend>

                  <tfoot id='Eh6R6'></tfoot>

                    <bdo id='Eh6R6'></bdo><ul id='Eh6R6'></ul>
                  <i id='Eh6R6'><tr id='Eh6R6'><dt id='Eh6R6'><q id='Eh6R6'><span id='Eh6R6'><b id='Eh6R6'><form id='Eh6R6'><ins id='Eh6R6'></ins><ul id='Eh6R6'></ul><sub id='Eh6R6'></sub></form><legend id='Eh6R6'></legend><bdo id='Eh6R6'><pre id='Eh6R6'><center id='Eh6R6'></center></pre></bdo></b><th id='Eh6R6'></th></span></q></dt></tr></i><div id='Eh6R6'><tfoot id='Eh6R6'></tfoot><dl id='Eh6R6'><fieldset id='Eh6R6'></fieldset></dl></div>

                  <small id='Eh6R6'></small><noframes id='Eh6R6'>