2012-02-20 131 views
2


我的表(S)结构(的MySQL /每个人都是下同)MySQL中INSERT INTO ... ON DUPLICATE KEY UPDATE的正确语法是什么?

+-------+--------------+------+------+-------------------+ 
| 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 

我'得到错误有任何编码错误?什么是解决方案?
由于现在...

+0

我不认为这就是整个的错误消息。也可以使用准备好的语句(请参阅mysqli扩展的文档) – Vitamin 2012-02-20 20:17:18

+0

或至少将值放在引号(')中。请提供所涉及变量的值或最终查询字符串(不含变量)。 – Basti 2012-02-20 20:20:20

回答

6
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 AI 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)"; 
+0

感谢您的救命答案... – Alper 2012-02-21 21:47:44

0

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

0

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

一个解决方案可能是,你读这里的考试等于您发送的参数表中,这样的事情:

SELECT id FROM $proper_table; 

如果是空的执行插入,如果它不是空的你以参数更新为参数您从选择中获得的ID

+1

考试是一个唯一的密钥,因此可能会有冲突 – 2012-02-20 20:28:33

0

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"; 
0

你必须要像这样

<?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()); 

} 
?> 
+0

你不需要,它的可读性就好多了 – 2012-02-20 20:45:06

相关问题