gpt4 book ai didi

php - 在 reddit 风格的投票系统中传递正确的 ID 和更新分数时遇到问题

转载 作者:行者123 更新时间:2023-11-29 00:23:10 24 4
gpt4 key购买 nike

我正在尝试为我的站点调整此 reddit 风格的投票系统:http://www.technabled.com/2009/02/reddit-style-voting-with-php-mysql-and.html

我在使用 votes.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 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 文件包含连接到数据库的 $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>

对于 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!";
}
?>

或在这里试一试:http://itu.dk/people/mbaj/dwdeksamen/rating.php?bidragId=1

最佳答案

您需要将 $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);

确保您阅读了 variable scope在 PHP 中,因为它应该为您解决一些问题。

关于php - 在 reddit 风格的投票系统中传递正确的 ID 和更新分数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20301943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com