2


我的表结构(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) 添加此表 -
database.php 中的 PHP 5.0 有一个函数可以获取发布的数据并添加到表中

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 

有没有错误的编码?什么是解决方案?
谢谢现在...

4

5 回答 5

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 (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)";
于 2012-02-20T20:19:15.530 回答
0

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

于 2012-02-20T20:20:11.773 回答
0

With that query, when you add ON DUPLICATE KEY UPDATE... it will update when the id es the same than the id that you are sending, in this case you are not sending an id as parameter so it will never update because you have the id with auto-increment.

A solution could be that you read the table where exam equals the parameter you are sending, something like this:

    SELECT id FROM $proper_table;

If it is null the you execute an insert, if it is not null the you update taking as parameter the id that you are getting from the select

于 2012-02-20T20:27:03.720 回答
0

id auto-increments, so presumably you don't want to set an empty string as id.

Try:

$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";
于 2012-02-20T20:29:39.483 回答
0

You have to make it like this

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

}
?>
于 2012-02-20T20:34:08.540 回答