jquery告诉我一些东西被添加到表中,但它没有


jquery telling me something is added to a table but it is not

我有一个表单,当您单击按钮时,该表单应该将数据添加到表中,并且会出现一个弹出窗口,告诉您它已添加或存在问题。用户可以将数据放入文本区域并单击速率,该框出现并告诉您它已添加,但是当我查看表格时,它尚未添加。浏览代码后,我看不到任何跳出来的东西。

这是表格

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<form id="add-rateing">
 <input type="text" name="MOVIE_ID">
  <input type="text" name="USER_ID">
<input type="submit" value="rate">
</form>
<script type="text/javascript">
$("#add-rateing").submit(function(){
event.preventDefault()
    addrateing();
   });
function addrateing()
{
var movie_id_s    = $("#add-rateing [name='MOVIE_ID']").val();
var user_id_s   = $("#add-rateing [name='USER_ID']").val();
var errors  = '';
$.ajax({
    type    : "GET",
    url     : "movie_watched.php",
    data    : { movie  : movie_id_s,
                user : user_id_s, },
    cache   : false, timeout: 10000,
    success  : function() {
        alert("added");
    },
    error    : function() {
        alert("there is a problom");
    },
    complete : function() {
    }
});
};
</script>
</body>
</html>

这是movie_watched.php

    <?php
include"scripts/connect.php" ;
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$movie = mysql_real_escape_string($_POST['MOVIE_ID']);
$user = mysql_real_escape_string($_POST['USER_ID']);
$error = '';
$query=mysql_query"INSERT INTO rateing (movie_id, user_id) VALUES ('".$movie."', '".$user."')";
if (!mysql_query($query, $conn))
{
$error = mysql_error();
$return['error'] = $error;
echo json_encode($return);
mysql_close($conn);
}
else
{
$success = "Thank you for playing";
$return['mysql'] = $success;
echo json_encode($return);
mysql_close($conn);
}
?>

谢谢。

$query=mysql_query"INSERT INTO rateing (movie_id, user_id) VALUES ('".$movie."', '".$user."')";

应该是:

$query=mysql_query("INSERT INTO rateing (movie_id, user_id) VALUES ('".$movie."', '".$user."')");

看起来好像你忘记了一个左括号。

我认为问题是您在$.ajax调用的上下文中误解了errorsuccesssuccess仅表示您的 ajax 调用已成功完成。 您现在需要做的是检查是否有错误。

$.ajax({
    type    : "GET",
    url     : "movie_watched.php",
    data    : { movie  : movie_id_s,
                user : user_id_s, },
    cache   : false, timeout: 10000,
    dataType : 'json',
    success  : function(json) {
        if (json.error){
            // your php call returned successfully, but set an error code
            alert('failed : ' + json.error);
        } else {
            alert("added");
        }
    },
    error    : function() {
        alert("there is a problom");
    },
    complete : function() {
    }
});