Php 尝试使用$wpdb类时无法在数据库中插入数据
我正在尝试创建一个投票系统,用户可以点击投票!但它将收集他们的Facebook用户id,这样一个人就不能在一篇帖子上提交多张投票! 这是我正在使用的代码-Php 尝试使用$wpdb类时无法在数据库中插入数据,php,mysql,database,wordpress,Php,Mysql,Database,Wordpress,我正在尝试创建一个投票系统,用户可以点击投票!但它将收集他们的Facebook用户id,这样一个人就不能在一篇帖子上提交多张投票! 这是我正在使用的代码- $(".vote i").on("click", function() { $this = $(this); var votePostID = $(this).closest(".scholarship-card").attr("id"); postID = votePostID; FB.getLoginS
$(".vote i").on("click", function() {
$this = $(this);
var votePostID = $(this).closest(".scholarship-card").attr("id");
postID = votePostID;
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
$this.closest(".vote").find("span").html("vote submitted!");
submitVote(postID);
FBAPI();
} else {
showLoginPopup();
}
});
});
function submitVote(post_id) {
$.ajax({
url: "voting.php",
type: "POST",
data: {postID: post_id, fbID: fb_id},
beforeSend: function() {
console.log("sending...");
},
success: function (data) {
console.log("done...");
console.log(data);
console.log(data.vote);
}
});
}
这里是PHP独立文件,它是根据ajax请求调用的-
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
$postID = $_POST["postID"];
$fbID = $_POST["fbID"];
$response = array();
$response["vote"] = "THIS IS WORKING...";
$fbID_fetch = $wpdb->get_var("SELECT postID FROM scholarship_votes_uid WHERE uid=$uid");
$response["fBID_fetch"] = $fbID_fetch;
if ( $fbID_fetch === null ) {
$wpdb->query("INSERT INTO scholarship_votes_uid(uid, postID) VALUE ($uid, $postID)");
/*$wpdb->insert('scholarship_votes_uid',
array(
'uid' => $fbID,
'postID' => $postID
),
array(
'%d',
'%d'
));*/
$wpdb->insert('scholarship_votes_count',
array(
'postID' => $postID,
'voteCount' => 0
),
array(
'%d',
'%d'
));
$wpdb->query("UPDATE scholarship_votes_count SET voteCount=voteCount+1 WHERE postID=$postID");
} else {
$response["error"] = "Already Voted!";
}
echo json_encode($response);
?>
请帮我找到解决办法。我真想修好这个投票系统 您使用的是空的$uid,而不是$fbID错误消息在哪里?请澄清您面临的问题到底是什么。实际上,错误是:当我调用ajax请求时,它正在处理php文件。文件实际上有一些代码,应该首先检查fb id是否存在!如果fb id不存在,那么它将把fb id和post id添加到一个表中,并将votecount和post id插入到另一个表中。但是当我运行当前的php代码时,它会更新votecount,但不会将fb id和post id添加到另一个表中,我基本上是用它来验证fb id是否已经投票!让我理解一下-您已经验证了脚本进入if语句并只执行最后一个查询了吗?前两个未执行?可能您使用的是空的$uid而不是$fbID?是的!现在我明白了。。。谢谢你,阿贝尔