【问题标题】:What is the correct syntax for INSERT INTO ... ON DUPLICATE KEY UPDATE in MySQL?MySQL 中 INSERT INTO ... ON DUPLICATE KEY UPDATE 的正确语法是什么?
【发布时间】:2012-02-20 20:14:52
【问题描述】:


我的表结构(MySQL / 每个都与下面相同)

+-------+--------------+------+------+-------------------+
| Field | Type         | Null | Key  | Default           |
+-------+--------------+------+------+-------------------+
| id    | int(11)      | NO   | PRI  | AUTO INCREMENT    | 
| lesson| varchar(255) | NO   |      | LESSON_NAME       | 
| exam  | char(50)     | NO   |UNIQUE| NO DEFAULT        |
| quest | text         | NO   |      | NO DEFAULT        |
| answer| text         | NO   |      | NO DEFAULT        |
| note  | text         | NO   |      | NO DEFAULT        |
+-------+--------------+------+------+-------------------+

我正在发布一些值以通过 ajax ($post) 添加此表 - PHP 5.0
在 database.php 中有一个函数可以获取发布的数据并添加到表中

function update_table ($proper_table, $name, $question, $answer, $note) {
$sql = "INSERT INTO $proper_table (id, lesson, exam, quest, answer, note) VALUES ('', '', $name, $question,$answer,$note) ON DUPLICATE KEY UPDATE exam = $name, quest = $question, answer = $answer, note = $note";
$result= mysql_query($sql)or die(mysql_error());
}

$proper_table 变量被另一个变量使用以将此记录添加到正确的表中。
(注意:原始表字段和变量不同(土耳其语),为了更容易理解,我翻译成英文,但语法与您看到的相同。)
问题:我想检查是否有一条记录与考试字段相同,那么所有这些变量都将用于更新此记录,否则让函数将此记录作为新记录放入适当的表中。
但我收到如下错误

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 

是否有任何错误的编码?有什么解决办法?
现在谢谢...

【问题讨论】:

  • 我不认为这是整个错误信息。也可以使用准备好的语句(参见 mysqli 扩展的文档)
  • 或至少将值放在引号 (') 中。请提供所涉及变量的值或最终查询字符串(不带变量)。

标签: php mysql on-duplicate-key


【解决方案1】:
function update_table ($proper_table, $name, $question, $answer, $note) {
    $sql = "INSERT INTO $proper_table (lesson, exam, quest, answer, note) VALUES ('', '$name', '$question','$answer','$note') ON DUPLICATE KEY UPDATE quest = VALUES(quest), answer = VALUES(answer), note = VALUES(note)";
   $result= mysql_query($sql)or die(mysql_error());
}

只要打破这个,我会详细说明变化

$sql = "INSERT INTO $proper_table 

// Removed the PK (primary key) AI (auto increment) field - don't need to specify this
(lesson, exam, quest, answer, note)     

// Likewise removed PK field, and added quotes around the text fields
VALUES ('', '$name', '$question','$answer','$note')    
ON DUPLICATE KEY UPDATE 

// If you specify VALUES(fieldName) it will update with the value you specified for the field in the conflicting row
// Also removed the exam update, as exam is the UNIQUE key which could cause conflicts so updating that would have no effect
quest = VALUES(quest), answer = VALUES(answer), note = VALUES(note)";

【讨论】:

    【解决方案2】:

    例如,您需要在 SQL '$name' 中将字符串变量用单引号括起来。否则 mysql 认为你在引用列名。

    【讨论】:

      【解决方案3】:

      使用该查询,当您添加 ON DUPLICATE KEY UPDATE... 它会在 id 与您发送的 id 相同时更新,在这种情况下,您不会发送 id 作为参数,因此它永远不会更新因为你有自动递增的 id。

      一种解决方案可能是您阅读表格,其中考试等于您发送的参数,如下所示:

          SELECT id FROM $proper_table;
      

      如果为空,则执行插入,如果不为空,则更新以从选择中获取的 id 作为参数

      【讨论】:

      • 考试是唯一键,因此可能会发生冲突
      【解决方案4】:

      id 自动递增,所以大概你不想将空字符串设置为id

      试试:

      $sql = "INSERT INTO $proper_table (lesson, exam, quest, answer, note) VALUES ('', $name, $question,$answer,$note) ON DUPLICATE KEY UPDATE exam = $name, quest = $question, answer = $answer, note = $note";
      

      【讨论】:

        【解决方案5】:

        你必须这样做

        <?php
        function update_table($proper_table, $name, $question, $answer, $note, $id) {
        
            $sqlQuery = "INSERT INTO '".$proper_table."' SET
                            name        =       '".$name."',
                            question        =   '".$question."',
                            answer      =       '".$answer."',
                            note        =       '".$note."' WHERE id = '".$id."'";
        
            $result= mysql_query($sqlQuery)or die(mysql_error());
        
        }
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-30
          • 1970-01-01
          • 2011-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-29
          • 1970-01-01
          相关资源
          最近更新 更多