为什么使用jQuery更新文本框后数据库不会自动更新?

我正在创建一个文本框,该文本在用户键入后无需提交便会自动更新为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);
 ?>
echo "<td class=\"align-center\"><input type=\"text\" name=\"comment\" id=\"comment\" onPressKey=\"getComment($id,this.id)\" required/></td>";

should be

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

Your query has multiple errors. It should be:

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

while mysql_* should NOT be used as they are deprecated. use MySQLi or PDO instead. Watch out for SQL Injection too.

Try to replace

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

with

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

EDIT:

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

And at your function grab the value like

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

You need to send the textbox value not again the id and try to update your update query like

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

Here $id is an integer na dyou have missed closing " at the last.And try to use mysqli_* statements or pdo statements instead of mysql_* statements due to they are deprecated

AND main thing make sure that comment column in DataBase should be data type text as per your comment

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

You use onPressKey instead of onKeyPress

Following is your code

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

Change this to

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

Basically when you pass values for javascript function if the value you pass is string or integer rather than variable enclose them in single quote like getComment('$id',this.value)

And another change that I added is onKeyup instead of onKeyPress. This is because onKeyPress event will happen when a key is pressed down and the function you called will not have the last character you keyed in. Whereas onKeyPress event will be triggered when you release the key i.e. after typing the character so you will get all the characters entered in the text box

similarly in Mysql query always enclose values in single quotes like following

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

Hope you understand those changes