尝试使用$ wpdb类时,无法在数据库中插入数据

时间:2017-05-18 18:40:50

标签: php mysql database wordpress

我正在尝试创建一个投票系统,用户可以点击投票!但它将收集他们的Facebook用户ID,以便一个人不能在帖子上提交多个投票! 这是我正在使用的代码 -

$(".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);
        }
    });
}

这是在ajax请求上调用的PHP独立文件 -

<?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);

?>

请帮我找到解决方案。我只是想修复这个投票系统!

1 个答案:

答案 0 :(得分:1)

您正在使用$uid(空的)代替$fbID