• <legend id='RKlWH'><style id='RKlWH'><dir id='RKlWH'><q id='RKlWH'></q></dir></style></legend>

    1. <tfoot id='RKlWH'></tfoot>

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

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

        <bdo id='RKlWH'></bdo><ul id='RKlWH'></ul>

        MYSQL 在 PDO 不存在的地方插入

        时间:2023-10-30

        • <legend id='8ARlP'><style id='8ARlP'><dir id='8ARlP'><q id='8ARlP'></q></dir></style></legend>

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

                <tfoot id='8ARlP'></tfoot>
              • <small id='8ARlP'></small><noframes id='8ARlP'>

                  本文介绍了MYSQL 在 PDO 不存在的地方插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 PHP PDO 命令有条件地将值插入 MySQL 数据库.条件是如果要插入的行已经存在,就不要插入.如果不存在,则插入该行.当我通过 PHPmyadmin 执行时,代码执行得很好,因此将 PDO 变量(:a:b)与静态数字交换.

                  I am conditionally inserting values to a MySQL database using a PHP PDO command. The condition states that if the row to be inserted already exists, do not insert it. If it does not exist, insert the row. The code performs just fine when I execute through PHPmyadmin, and thus swap the PDO variables (:a, :b) with static numbers.

                  我收到以下错误:

                  Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name '0'' in...
                  

                  PHP代码如下:

                  我准备sql语句...

                  I prepare the sql statement...

                  $InsertFoo = $pdo->prepare("
                  
                      INSERT INTO `MyTbl` (
                                              `ColA`,
                                              `ColB`
                                              )
                      SELECT * FROM ( SELECT :a, :b ) AS tmp
                      WHERE NOT EXISTS (
                          SELECT `MyTbl`.`ColA`
                          FROM `MyTbl`
                              WHERE `ColA` = :aa
                              AND `ColB` = :bb
                          )
                      LIMIT 1
                  
                  ");
                  

                  然后循环遍历一个数组来一遍又一遍地执行准备好的语句.(该数组之前是使用 SimpleXML 生成的,因此使用了 ->.如果您不熟悉 SimpleXML,只需将 $foo->fooA 视为一个可以更改的变量每个循环.)

                  And then loop through an array to execute the prepared statement over and over. (The array was generated earlier using SimpleXML, hence the ->. If you're unfamiliar with SimpleXML, just consider $foo->fooA as a variable that changes for each loop.)

                  foreach ($loopme as $foo) {
                  
                      // query to be executed for ticket insertion
                      $TicketsToInsert->execute(array(
                                                      a => $foo->fooA,
                                                      b => $foo->fooB,
                                                      aa => $foo->fooA,
                                                      bb => $foo->fooB
                                                      ));
                  
                  }
                  

                  非常感谢任何建议:

                  附带说明一下,有很多类似的问题,但我还没有找到任何可以在插入时完全处理此 PDO 问题的问题.

                  As a side note, there are many similar questions, but I have not found any that exactly deal with this PDO issue on insert.

                  1. PDO 一次只能执行一条语句
                  2. 同样的错误,似乎成为不同的来源
                  3. 选择中的 PDO 变量后跟 Where 声明

                  非常感谢!

                  已回答

                  Phil 提供了一种更简单的解决方案,即跨多行应用唯一约束,这反过来会拒绝新插入的在所述行中唯一的行.这与测试将要插入的行是否不存在相反.更多详情如下.

                  Phil offered the much simpler solution of applying a unique constraint across multiple rows, which would in turn reject a newly inserted row that was unique across said rows. This as opposed to testing for the non existence of the row that is about to be inserted. More details below.

                  推荐答案

                  您的查询看起来完全一团糟,尤其是在您的 EXISTS 子查询中.您正在从 tickets???

                  Your query looks completely messed up, especially in your EXISTS sub-query. You're selecting MyTbl.ColA from tickets???

                  我的建议是简单地在 MyTbl (ColA, ColB) 上添加一个唯一约束.

                  My advice would be to simply add a unique constraint on MyTbl (ColA, ColB).

                  ALTER TABLE MyTbl ADD UNIQUE (ColA, ColB);
                  

                  然后,您的 INSERT 将失败,并出现 唯一的约束冲突,这可能会被 PDOException 捕获.

                  Then, your INSERT will fail with a unique constraint violation which can be caught in a PDOException.

                  $stmt = $pdo->prepare('INSERT INTO MyTbl (ColA, ColB) VALUES (?, ?)');
                  foreach ($loopme as $foo) {
                      try {
                          $stmt->execute([$foo->fooA, $foo->fooB]);
                      } catch (PDOException $e) {
                          $errorCode = $stmt->errorInfo()[1];
                          if ($errorCode == 1586) {
                              // I think 1586 is the unique constraint violation error.
                              // Trial and error will confirm :)
                          } else {
                              throw $e;
                          }
                      }
                  }
                  

                  解决您看到的错误消息...这是因为您没有区分 INSERT 表和子查询表.

                  To address the error message you're seeing... it's because you aren't differentiating between the INSERT table and the sub-query table.

                  这篇关于MYSQL 在 PDO 不存在的地方插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:生成两个日期之间的日期列表并将列表保存在数据库中 下一篇:从文本框中的逗号分隔文本在 MYSQL 中添加新行

                  相关文章

                    • <bdo id='q2xH1'></bdo><ul id='q2xH1'></ul>

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

                    1. <legend id='q2xH1'><style id='q2xH1'><dir id='q2xH1'><q id='q2xH1'></q></dir></style></legend>
                      <tfoot id='q2xH1'></tfoot>

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