Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 插入到…的正确语法是什么。。。MySQL中的重复密钥更新?_Php_Mysql_On Duplicate Key - Fatal编程技术网

Php 插入到…的正确语法是什么。。。MySQL中的重复密钥更新?

Php 插入到…的正确语法是什么。。。MySQL中的重复密钥更新?,php,mysql,on-duplicate-key,Php,Mysql,On Duplicate Key,我的表结构(MySQL/每个表如下) 我正在通过ajax($post)-PHP5.0发布一些值来添加这个表 在database.php中,有一个获取发布数据并添加到表中的函数 function update_table ($proper_table, $name, $question, $answer, $note) { $sql = "INSERT INTO $proper_table (id, lesson, exam, quest, answer, note) VALUES ('', ''


我的表结构(MySQL/每个表如下)

我正在通过ajax($post)-PHP5.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());
}
$PROPERT_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 
有没有错误的编码?解决方案是什么?
现在就谢谢你

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

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

对于该查询,当您添加重复键更新时。。。当id与您正在发送的id相同时,它将更新,在这种情况下,您没有将id作为参数发送,因此它将永远不会更新,因为您的id具有自动增量

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

    SELECT id FROM $proper_table;

如果为空,则执行插入;如果不为空,则更新参数为您从select

id
自动递增中获得的id,因此您可能不希望将空字符串设置为
id

尝试:


你必须这样做

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

}
?>

我认为这不是全部错误信息。也可以使用预先准备好的语句(请参阅mysqli扩展的文档),或者至少将值放在引号(')中。请提供所涉及变量的值或最终查询字符串(无变量)。exam是唯一的键,因此可能存在冲突
$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";
<?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());

}
?>