我的表结构(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
有没有错误的编码?什么可以解决方案?
谢谢你......
答案 0 :(得分: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)";
答案 1 :(得分:0)
例如,您需要在SQL '$name'
中用单引号将字符串变量包装起来。否则mysql认为你引用的是列名。
答案 2 :(得分:0)
使用该查询,当您添加ON DUPLICATE KEY UPDATE ...当id与您发送的id相同时,它将更新,在这种情况下,您不会发送id作为参数,因此它永远不会更新因为你有自动增量的id。
解决方案可能是您阅读了表格,其中考试等于您要发送的参数,如下所示:
SELECT id FROM $proper_table;
如果它为null,则执行插入,如果它不为null,则更新将参数作为参数从选择中获取
答案 3 :(得分: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";
答案 4 :(得分: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());
}
?>