Вопрос

I'm trying to adapt this reddit-style voting-system for my site: http://www.technabled.com/2009/02/reddit-style-voting-with-php-mysql-and.html

I'm having trouble with the votes.php script that is supposed to fetch the plus or minus vote and update the score.

It gives the following errors:

Notice: Undefined variable: dbc in /net/fshome1.itu.dk/export/home1/mbaj/public_html/dwdeksamen/votes.php on line 14 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /net/fshome1.itu.dk/export/home1/mbaj/public_html/dwdeksamen/votes.php on line 14 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /net/fshome1.itu.dk/export/home1/mbaj/public_html/dwdeksamen/votes.php on line 15 Notice: Undefined offset: 0 in /net/fshome1.itu.dk/export/home1/mbaj/public_html/dwdeksamen/votes.php on line 44 Notice: Undefined variable: plus in /net/fshome1.itu.dk/export/home1/mbaj/public_html/dwdeksamen/votes.php on line 45 Failed!

I think I'm mixing something up between the $bidragId and the $id variables. At the same time I can't seem to figure out why it doesn't want to accept the $dbc variable that connects to the the database through my mysqli_connect.php fileinclude.

Here's the code for rating.php:

<?php
error_reporting(E_ALL);
    ini_set('display_errors', '1');
// Connecting to DB
    require_once ('mysqli_connect.php');

$bidragId = $_GET['bidragId'];

?>

<html>
<head>
    <title>Votes</title>

<script type='text/javascript' src='jquery.pack.js'></script>
<script type='text/javascript'>
$(function(){
    $("a.plus").click(function(){
    //get the id
    the_id = $(this).attr('id');

    // show the spinner
    $(this).parent().html("<img src='images/spinner.gif'/>");

    //fadeout the vote-count 
    $("span#votes_count"+the_id).fadeOut("fast");

    //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=plus&id="+$(this).attr("id"),
            url: "votes.php",
            success: function(msg)
            {
                $("span#votes_count"+the_id).html(msg);
                //fadein the vote count
                $("span#votes_count"+the_id).fadeIn();
                //remove the spinner
                $("span#vote_buttons"+the_id).remove();
            }
        });
    });

    $("a.minus").click(function(){
    //get the id
    the_id = $(this).attr('id');

    // show the spinner
    $(this).parent().html("<img src='images/spinner.gif'/>");

    //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=minus&id="+$(this).attr("id"),
            url: "votes.php",
            success: function(msg)
            {
                $("span#votes_count"+the_id).fadeOut();
                $("span#votes_count"+the_id).html(msg);
                $("span#votes_count"+the_id).fadeIn();
                $("span#vote_buttons"+the_id).remove();
            }
        });
    });
}); 
</script>

<style type="text/css">
    body {  
 background: #e8e6de;  
}  

a {  
outline:none;  
}  

.entry {  
 width: 710px;  
 background: #ffffff;  
 padding:8px;  
 border:1px solid #bbbbbb;  
 margin:5px auto;  
 -moz-border-radius:8px;  
}  

span.link a {  
 font-size:150%;  
 color: #000000;  
 text-decoration:none;  
}  

a.vote_up, a.vote_down {  
 display:inline-block;  
 background-repeat:none;  
 background-position:center;  
 height:16px;  
 width:16px;  
 margin-left:4px;  
 text-indent:-900%;  
}  

a.vote_up {  
 background:url("images/thumb_up.png");  
}  

a.vote_down {  
 background:url("images/thumb_down.png");  
}
</style>

</head>
<body>

<?php
/**
Display the results from the database
**/
error_reporting(E_ALL);
ini_set('display_errors', '1');
$q = "SELECT * FROM rating WHERE bidragId = $bidragId";
$r = mysqli_query($dbc, $q);

if(mysqli_num_rows($r)>0): //table is non-empty
    while($row = mysqli_fetch_assoc($r)):
        $net_vote = $row['plus'] - $row['minus']; //this is the net result of voting up and voting down
?>
<div class='entry'>

    <span class='votes_count' id='votes_count<?php echo $row['bidragId']; ?>'><?php echo $net_vote." votes"; ?></span>

    <span class='vote_buttons' id='vote_buttons<?php echo $row['bidragId']; ?>'>
        <a href='javascript:;' class='plus' id='<?php echo $row['bidragId']; ?>'>Plus</a>
        <a href='javascript:;' class='minus' id='<?php echo $row['bidragId']; ?>'>Minus</a>
    </span>

</div>
<?php
    endwhile;
endif;
?>


</body>
</html>

And for votes.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
// Connecting to DB
require_once ('mysqli_connect.php');

function getAllVotes($id)
    {
    /**
    Returns an array whose first element is votes_up and the second one is votes_down
    **/
    $votes = array();
    $q = "SELECT * FROM rating WHERE bidragId = $bidragId";
/** LINE 14 **/ $r = mysqli_query($dbc, $q);
/** LINE 15 **/ if(mysqli_num_rows($r)==1)//id found in the table
        {
        $row = mysqli_fetch_assoc($r);
        $votes[0] = $row['plus'];
        $votes[1] = $row['minus'];
        }
    return $votes;
    }

function getEffectiveVotes($id)
    {
    /**
    Returns an integer
    **/
    $votes = getAllVotes($id);
    $effectiveVote = $votes[0] - $votes[1];
    return $effectiveVote;
    }

$id = $_POST['id'];
$action = $_POST['action'];

//get the current votes
$cur_votes = getAllVotes($id);

//ok, now update the votes

if($action=='plus') //voting up
{
/** LINE 44 **/ $votes_up = $cur_votes[0]+1;
/** LINE 45 **/ $q = "UPDATE rating SET plus = $plus WHERE bidragId = $id";
}
elseif($action=='minus') //voting down
{
    $votes_down = $cur_votes[1]+1;
    $q = "UPDATE rating SET minus = $minus WHERE bidragId = $id";
}

$r = mysqli_query($dbc, $q);
if($r) //voting done
    {
    $effectiveVote = getEffectiveVotes($id);
    echo $effectiveVote."";
    }
elseif(!$r) //voting failed
    {
    echo "Failed!";
    }
?>

Or give it a try here: http://itu.dk/people/mbaj/dwdeksamen/rating.php?bidragId=1

Это было полезно?

Решение

You'll need to pass $dbc in as a function parameter as $dbc is not declared inside the function's local variable scope:

function getAllVotes($id, $dbc){
    /**
    Returns an array whose first element is votes_up and the second one is votes_down
    **/
    $votes = array();
    $q = "SELECT * FROM rating WHERE bidragId = $bidragId";
    /** LINE 14 **/ 
    $r = mysqli_query($dbc, $q);
    /** LINE 15 **/ 
    if(mysqli_num_rows($r)==1)//id found in the table{
        $row = mysqli_fetch_assoc($r);
        $votes[0] = $row['plus'];
        $votes[1] = $row['minus'];
    }
    return $votes;
}

And when you're calling it:

$cur_votes = getAllVotes($id, $dbc);

Make sure that you read up on variable scope in PHP as it should clear up a few things for you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top