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

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

そのクエリでは、ON DUPLICATE KEY UPDATE を追加すると... ID が送信している ID と同じ場合に更新されます。この場合、ID をパラメーターとして送信していないため、更新されません。自動インクリメント付きの id。

解決策は、exam が送信するパラメータと等しい表を読むことです。次のようになります。

    SELECT id FROM $proper_table;

null の場合は挿入を実行し、null でない場合は select から取得した ID をパラメーターとして更新します

于 2012-02-20T20:27:03.720 に答える
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());

}
?>
于 2012-02-20T20:34:08.540 に答える
0

'$name'たとえば、SQL で文字列変数を一重引用符で囲む必要があります。そうしないと、mysql は、列名を参照していると見なします。

于 2012-02-20T20:20:11.773 に答える