Php 在reddit风格的投票系统中,无法通过正确的ID和更新分数
我正在尝试将这种reddit风格的投票系统应用于我的网站: 我在vows.php脚本中遇到了问题,该脚本应该获取加号或减号投票并更新分数 它给出了以下错误:Php 在reddit风格的投票系统中,无法通过正确的ID和更新分数,php,mysql,voting-system,Php,Mysql,Voting System,我正在尝试将这种reddit风格的投票系统应用于我的网站: 我在vows.php脚本中遇到了问题,该脚本应该获取加号或减号投票并更新分数 它给出了以下错误: 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 mysq
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!
我想我把$bidragId和$id变量搞混了。同时,我似乎不明白为什么它不想接受通过mysqli_connect.php fileinclude连接到数据库的$dbc变量
下面是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>
投票
$(函数(){
$(“a.plus”)。单击(函数(){
//拿到身份证
_id=$(this.attr('id');
//展示旋转器
$(this.parent().html(“”);
//淡出计票
$(“选票数”+id)。淡出(“快速”);
//主要的ajax请求
$.ajax({
类型:“POST”,
数据:“action=plus&id=“+$(this).attr(“id”),
url:“vows.php”,
成功:功能(msg)
{
$(“span#voces _count”+the _id).html(msg);
//计票
$(“span#voces_count”+该id).fadeIn();
//卸下旋转器
$(“投票按钮”+投票id).remove();
}
});
});
$(“a.减号”)。单击(函数(){
//拿到身份证
_id=$(this.attr('id');
//展示旋转器
$(this.parent().html(“”);
//主要的ajax请求
$.ajax({
类型:“POST”,
数据:“action=减号&id=“+$(this).attr(“id”),
url:“vows.php”,
成功:功能(msg)
{
$(“span#voces_count”+该id).fadeOut();
$(“span#voces _count”+the _id).html(msg);
$(“span#voces_count”+该id).fadeIn();
$(“投票按钮”+投票id).remove();
}
});
});
});
正文{
背景:#e8e6de;
}
a{
大纲:无;
}
.条目{
宽度:710px;
背景:#ffffff;
填充:8px;
边框:1px实心#bbbbbb;
保证金:5px自动;
-moz边界半径:8px;
}
span.link a{
字体大小:150%;
颜色:#000000;
文字装饰:无;
}
a、 投赞成票,投反对票
显示:内联块;
背景重复:无;
背景位置:中心;
高度:16px;
宽度:16px;
左边距:4倍;
文本缩进:-900%;
}
a、 投票表决{
背景:url(“images/thumb_up.png”);
}
a、 投票否决{
背景:url(“images/thumb_down.png”);
}
对于vots.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!";
}
?>
或者在此处尝试:您需要将
$dbc
作为函数参数传入,因为$dbc
未在函数的局部变量范围内声明:
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;
}
当你叫它的时候:
$cur_votes = getAllVotes($id, $dbc);
请务必阅读PHP,因为它会为您澄清一些事情。您需要将
$dbc
作为函数参数传入,因为$dbc
未在函数的局部变量范围内声明:
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;
}
当你叫它的时候:
$cur_votes = getAllVotes($id, $dbc);
请务必阅读PHP,因为它会为您澄清一些事情。您可能想了解变量在PHP中有一个称为“作用域”的事实:您可能想了解变量在PHP中有一个称为“作用域”的事实:非常感谢!这肯定有帮助。但是现在,正如您在链接中看到的那样,rating.php-page一直在加载,没有更新投票数。它似乎也没有传递给DB。它从vows.php接收到500个内部错误。请确保启用了PHP错误报告,以便能够正确调试该问题。非常感谢!这肯定有帮助。但是现在,正如您在链接中看到的那样,rating.php-page一直在加载,没有更新投票数。它似乎也没有传递给DB。它从vows.php接收到500个内部错误。确保启用了PHP错误报告,以便能够正确调试问题。