我有一个喜欢按钮,它允许用户喜欢我网站上的帖子。如果用户喜欢他们之前不喜欢的帖子,它将 +1,如果他们再次按下相同的喜欢按钮,它将 -1。这在我笔记本电脑上的虚拟服务器上工作。但是,相同的代码在我的实时站点上不起作用。在我的实时网站上,用户可以多次喜欢同一个帖子,这不是我想要的。我正在使用 JQuery Ajax 调用来触发一些 MySQL 代码的 PHP 文件。
任何人都可以看到下面的 PHP 有什么明显的错误吗?
include ("../con/config.php");
$postid = $_POST['postid'];
$userid = $_POST['userid'];
$query = $con->prepare("SELECT COUNT(*) AS CntPost FROM Likes WHERE UserID = ? AND PostID = ?");
$query->bind_param('ss',$userid,$postid);
$query->execute();
$result = $query->get_result();
$fetchdata = $result->fetch_assoc();
$count = $fetchdata['CntPost'];
if($count == 0){
$stmt = $con->prepare("INSERT INTO Likes(UserID,PostID) VALUES(?,?)");
$stmt->bind_param("ss", $userid, $postid);
$stmt->execute();
} else {
$stmt = $con->prepare("DELETE FROM Likes WHERE UserID = ? AND PostID = ?");
$stmt->bind_param("ss", $userid, $postid);
$stmt->execute();
}
// count numbers of likes in post
$query = $con->prepare("SELECT COUNT(*) AS CntLike FROM Likes WHERE PostID = ?");
$query->bind_param('s', $postid);
$query->execute();
$result = $query->get_result();
$fetchlikes = $result->fetch_assoc();
$totalLikes = $fetchlikes['CntLike'];
$return_arr = array("likes"=>$totalLikes,"type"=>$count);
echo json_encode($return_arr);
慕田峪4524236
HUH函数