我的jQuery自动保存正在运行成功函数,但不会更新MySQL数据库。我做错了什么?
jQuery的:
function autosave() {
var t = setTimeout("autosave()", 5000);
var translation = $("#doc-translation").val();
if (translation.length > 0) {
$.ajax({
type: "POST",
url: "update-draft-submission.php",
data: translation,
cache: false,
success: function() {
$(".autosaved").empty().append("saved");
}
});
}
}
PHP:
<?php
session_start();
//retrieve our data
$iddoc = $_GET['iddoc'];
$trans = translation;
$transowner = $_SESSION['userid'];
$true = 1;
include "../dbconnect.php";
$query = "UPDATE translations
SET trans='$trans'
WHERE iddoc='$iddoc'
AND transowner='$transowner'";
mysqli_query($query);
mysqli_close();
echo "Saved";
?>
答案 0 :(得分:1)
您没有正确地获取PHP中的数据:
$iddoc = $_GET['iddoc'];
$trans = translation;
如果查询中没有获得所需的值,您的SQL就会中断。
更新您的javascript:
$.ajax(
{
type: "POST",
url: "update-draft-submission.php",
data: data: {translation:translation,iddoc:"XXX"},
cache: false,
success: function()
{
$(".autosaved").empty().append("saved");
}
});
将 XXX 替换为您的iddoc
值。
然后在PHP中将它们取为:
$iddoc = $_POST['iddoc'];
$trans = $_POST['translation'];