使用Mysql,Jquery和Php向上投票

时间:2016-08-02 18:11:01

标签: php jquery mysql ajax vote-up-buttons

我正在尝试使用php,mysql和jquery制作投票上调系统。它在前端完美运行,但在后端不会在数据库中添加数据。任何帮助/建议将不胜感激.. !!以下是代码..

<span id="links-<?php echo $rec1['que_id']; ?>">
<input type="hidden" id="votes-<?php echo $rec1['que_id']; ?>" value="<?php echo $rec1['votes']; ?>">

<?php

$vote_rank = 0;
$query ="SELECT SUM(vote_rank) as vote_rank FROM cvotes WHERE que_id = '".$rec1['que_id']."' and username = '$logged_user'";
 $result2 = $conn->query($query);
    foreach ($result2 as $roww) {
$up = "";
$down = "";

if(!empty($roww["vote_rank"])) {
    $vote_rank = $roww["vote_rank"];
    if($vote_rank == -1) {
    $up = "enabled";
    $down = "disabled";
    }
    if($vote_rank == 1) {
    $up = "disabled";
    $down = "enabled";
    }
}
?>  

<input type="hidden" id="vote_rank_status-<?php echo $rec1['que_id']; ?>" value="<?php echo $vote_rank; ?>">
<span class="btn-votes">
<input type="button" title="Up" class="up" onClick="addVote(<?php echo $rec1['que_id']; ?>,'1')" <?php echo $up; ?> />
<span class="label-votes"><?php echo $rec1['votes']; ?></span>
<input type="button" title="Down" class="down" onClick="addVote(<?php echo $rec1['que_id']; ?>,'-1')" <?php echo $down; ?> />
<p id='show'></p>
</span>

    function addVote(que_id,vote_rank){

    $.ajax({
    data:'que_id='+que_id+'&vote_rank='+vote_rank,
    url: "add_vote.php",
    type: "POST",
    beforeSend: function(){
        $('#links-'+que_id+' .btn-votes').html("<img src='LoaderIcon.gif' />");
    },
    success: function(vote_rank_status){
    var votes = parseInt($('#votes-'+que_id).val());
    var vote_rank_status;// = parseInt($('#vote_rank_status-'+que_id).val());
    switch(vote_rank) {
        case "1":
        votes = votes+1;
        vote_rank_status = vote_rank_status+1;
        break;
        case "-1":
        votes = votes-1;
        vote_rank_status = vote_rank_status-1;
        break;
    }
    $('#votes-'+que_id).val(votes);
    $('#vote_rank_status-'+que_id).val(vote_rank_status);

    var up,down;

    if(vote_rank_status == 1) {
        up="disabled";
        down="enabled";
    }
    if(vote_rank_status == -1) {
        up="enabled";
        down="disabled";
    }   
    var vote_button_html = '<input type="button" title="Up" class="up" onClick="addVote('+que_id+',\'1\')" '+up+' /><span class="label-votes">'+votes+'</span><input type="button" title="Down" class="down"  onClick="addVote('+que_id+',\'-1\')" '+down+' />';    
    $('#links-'+que_id+' .btn-votes').html(vote_button_html);
    }
    });
}

  <?php
//-----add_vote.php-----
if(!empty($que_id)) {
     if(isset($_SESSION['login_user']))
    {
        $logged_user = $_SESSION['login_user'];
    }

$que_id=$_POST["que_id"];
    $vote_rank = $_POST["vote_rank"];

    require_once("dbcontroller.php");
    $db_handle = new DBController();


    $query = "INSERT INTO cvotes (que_id,username,vote_rank) VALUES ('$que_id','$logged_user','$vote_rank')";

    $result = $db_handle->insertQuery($query);


    if(!empty($result)) {
        $query = "SELECT SUM(vote_rank) as vote_rank FROM cvotes  WHERE que_id = '$que_id' and username = '$logged_user'";

        $row = $db_handle->runQuery($query);

        switch($vote_rank) {
            case "1":
                $update_query ="UPDATE questions SET votes = votes+1 WHERE que_id='" . $que_id . "'";
            break;
            case "-1":
                $update_query ="UPDATE questions SET votes = votes-1 WHERE que_id='" . $que_id . "'";
            break;
        }

        $result = $db_handle->updateQuery($update_query);   
        print $roww["vote_rank"];
    }
}
?>

2 个答案:

答案 0 :(得分:0)

似乎你的插入查询是错误的$(e.target).find('.project').each(function () { var detail = new _detailMozaik2.default($(this)); detail.init(); projects.push(detail); });,尝试根据数据类型使用引号..你给了所有值的引号,我认为除了用户名rest是整数数据类型然后更改查询如

@@ -l,s +l,s @@


l : starting line number

s : number of lines the change applies to

- : original file

+ : modified or new file

答案 1 :(得分:0)

好的,所以终于问题解决了!! !!只是变量的小错误和链接..下面是代码..

添加隐藏输入:     “&GT;

add_vote.php中的一些更改:

<?php
include('../connection.php');
 session_start();
if(isset($_SESSION['login_user']))
{
  $logged_u = $_SESSION['login_user'];
} 

if(!empty($_POST["que_id"])) {
require_once("dbcontroller.php");
$db_handle = new DBController();

$query = "INSERT INTO cvotes (username,que_id,vote_rank) VALUES ('".$logged_u."','" . $_POST["que_id"] . "','" . $_POST["vote_rank"] . "')";
$result = $db_handle->insertQuery($query);

if(!empty($result)) {

$query = "SELECT SUM(vote_rank) as vote_rank FROM cvotes  WHERE que_id = '" . $_POST["que_id"] . "' and username = '".$logged_u."' ";
    $row = $db_handle->runQuery($query);

    switch($_POST["vote_rank"]) {
        case "1":
            $update_query ="UPDATE questions SET votes = votes+1 WHERE que_id='" . $_POST["que_id"] . "'";
        break;
        case "-1":
            $update_query ="UPDATE questions SET votes = votes-1 WHERE que_id='" . $_POST["que_id"] . "'";
        break;
    }

    $result = $db_handle->updateQuery($update_query);   
    print $roww["vote_rank"];
   }
}
?>