通过AJAX传递的变量有什么特别之处吗?

时间:2020-04-04 17:08:58

标签: javascript php sql ajax

我正在尝试访问数据库并删除用户的评论,我有一种方法可以传递用户的ID和评论的ID。使用SQL命令以及调用硬编码变量时,此方法均能正常运行,但是,当我通过AJAX传递代码时,我的代码表示该代码已成功完成,但实际上并未执行任何操作。通过AJAX传递的变量有什么特别之处吗?

这是我的方法

 public function deleteRating($userid, $reviewID)
    {
        echo "this is idUsers(IdUsers) = ".$userid." this is reviewID (ID)".$reviewID;
        $conn = $this->connect("ratings");
        $sql = "DELETE FROM ratedmovies WHERE IdUsers=? AND ID=?";

        if(!$stmt = $conn->prepare($sql))
        {
            echo "False";
        }
        else
        {
            $stmt->bind_param("ss", $userid, $reviewId);
            if(!$stmt->execute())
            {
                echo "Failed to delete";
            }
            else
            {
                echo "Sucessfull Deletion";
            }
        }
    }

这是调用该方法的代码:

<?php
   session_start();
   include "../Model/Includes/autoLoadCont.inc.php";
   $reviews = new Review;
   $ratingID = json_decode($_POST['ratingID']);
   $user = $_SESSION['userId'];
   $reviews->deleteRating($user, $ratingID);
?>

这是调用该函数的ajax:

var deleteBtns = document.querySelectorAll(".deleteRating");
deleteBtns.forEach(function(button)
{
    button.addEventListener("click" , function()
    {
        $.ajax({
            type: "POST",
            url: "Controller/deleteReview.php",
            data: {ratingID:button.id},
            success: function(result)
            {
                alert(result);
            }
        });
    });
    button.id;
});

0 个答案:

没有答案