我正在一个非常基本的网站上工作,该网站允许人们每24小时对每个IP地址的项目(游戏)进行一次投票。投票存储在名为votes的表中,其中包含投票ID,游戏ID,投票和IP地址的列。
用户通过... vote.php?id =#(其中#是gid)进入该游戏的投票页面。这是我当前的vote.php的简化版本,它不起作用:
<?php
$gid = $_GET["id"];
$ip = $_SERVER['REMOTE_ADDR'];
require_once ('config.php');
$q = "SELECT * FROM votes WHERE (gid=$gid)";
$r = @mysqli_query($dbc, $q);
if ($r) {
while ($row = mysqli_fetch_array($r)) {
if ($row['ip'] != $ip) { // If no IP exists, go ahead and vote.
$q2 = "INSERT INTO votes (gid, votedate, ip) VALUES ('$gid', NOW(), '$ip' )";
$r2 = @mysqli_query($dbc, $q2);
if ($r2) {
echo 'Thank you for voting';
} else {
echo 'There was a problem with your vote'
}
} elseif ($ip == $row['ip']) {
// If IP address exists, check date of last time they voted for this game
// Define the two dates
$votedate = $row['votedate'];
$now = date("Y-m-d H:i:s");
// Calculate hours between the dates
$diff = round((strtotime($now) - strtotime($votedate)) / (60 * 60));
// Create variable for when user can return.
$return = '24' - $diff;
if ($diff > '24') { // Allow to vote once every 24 hours
$q3 = "INSERT INTO votes (gid, votedate, ip) VALUES ('$gid', NOW(), '$ip' )";
$r3 = @mysqli_query($dbc, $q3);
// Display outcome of insert query:
if ($r3) {
echo 'Thank you for voting, please come back in 24 hours to vote again!';
} else {
echo 'There was a problem with your vote:' . mysqli_error($dbc) . ' in reference to query ' . $q3;
}
} else {
echo 'Sorry, you\'ve already voted in past 24 hours, please come back again in ' . $return . ' hours.';
}
}
} // End of while $r loop
} else {
echo 'Sorry, your query did not work.';
} // End of overall if $r ran
mysqli_close($dbc);
exit();
?>
我的目标是让它起作用:
从网址抓取gid,找到与该gid相关的所有投票。如果用户之前从未投票给过该gid,则允许添加新的投票。如果用户投票支持该gid 检查先前投票的日期。如果最后一次投票超过24小时,则允许为该gid投票。如果最后一次投票是在24小时内,则拒绝对该游戏进行新的投票。
喜欢听到你的想法...
我看过其他投票系统,但我无法使jQuery,AJAX等系统适应我想要的,所以我认为一个非常基本的PHP系统最适合我有限的理解。我还希望能够仅列出过去30天内投票的投票,其他投票插件的数据库设计也不允许这样做。不过,如果有人有更好的选择,我会很高兴 - 因为我知道这个人很笨重。
干杯!
答案 0 :(得分:0)
小心,对于使用相同类型的数学运算:
尝试使用floatval()
$diff = floatval(round((strtotime($now) - strtotime($votedate)) / (60 * 60)));
$return = 24.0 - $diff;
if ($diff > 24.0)
{
/* Your code */
}