0

ユーザーの入力後、送信せずにテキストがmysqlに自動更新されるテキストボックスを作成しています。問題は、テキストボックスがデータベースを更新しないことです。これは、以下のすべての意見を参照して編集したものです。まだ問題がありますが、何も表示されないよりはましです。ありがとうございます。

ここにテキストボックスの入力があります

  echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyPress=\"getComment($id,this)\" /></td>";

ここに私のJavaScriptがあります

function getComment(id, txt_box)
{
var value = txt_box.value;

    $.ajax({
        'url': 'updatecomment.php',
        'data': {"id": id, "comment": value},
        'success': function (response) {
            console.log(response);
            //TODO: use server response
        }
    });
}

最後は updatecomment.php です

 <?php require_once("../includes/connection.php") ?>
 <?php require_once("../includes/functions.php") ?>

 <?php
     $id =$_GET['id'];
     $comment = $_GET['comment'];


     $query = "UPDATE tblinternapplication SET comment = '".mysql_real_escape_string($comment) ."' WHERE id = $id";
     $result = mysql_query($query, $connection);
 ?>
4

5 に答える 5

1

クエリに複数のエラーがあります。そのはず:

$query = "UPDATE tblinternapplication SET comment = '".mysql_real_escape_string($comment) ."' WHERE id = $id";

whilemysql_*は非推奨であるため、使用しないでください。代わりに MySQLi または PDO を使用してください。SQL インジェクションにも注意してください。

于 2013-06-05T08:59:33.210 に答える
1

交換してみる

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onPressKey=\"getComment($id,this.id)\" required/></td>";

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyPress=\"getComment($id,this.value)\" required/></td>";

編集:

echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyPress=\"getComment($id,this)\" required/></td>";

そして、あなたの関数で次のような値を取得します

function getComment(txt_box, comment) {
    var value = txt_box.value;
}

IDではなくテキストボックスの値を送信し、次のように更新クエリを更新する必要があります

$query = "UPDATE tblinternapplication 
          SET comment = ".mysql_real_escape_string($comment) ."
          WHERE id = $id";

ここで $id は整数であり、最後に"を閉じませんでした。また、非推奨になっているため、mysql_* ステートメントの代わりに mysqli_* ステートメントまたは pdo ステートメントを使用してみてください。

そして主なことは、DataBaseのコメント列がコメントに従ってデータ型テキストであることを確認することです

于 2013-06-05T08:59:40.897 に答える
1
echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onKeyPress=\"javascript: return getComment($id,this.value);\" required/></td>";

onPressKey代わりに使用しますonKeyPress

于 2013-06-05T09:09:28.810 に答える