为了让我的孩子们开心,我一直在PHP中实施Yellow Car Game以进行长途旅行。其中一个人坐在我的手机上,并在他们看到它们时添加黄色汽车。计数器应该增加每个分数,每周日晚上重置分数并显示上周的胜利者。
上周正确递增,本周再次正常,但周日我注意到所有得分都是0,而我上周的得分根本不是总和。
<?php
$host = "localhost";
$un = "username";
$pw = "password";
$db = "database";
$con = mysqli_connect($host, $un, $pw, $db);
$thisweek = date('Y-m-d', strtotime('Monday this week'));
//echo $thisweek;
$lastweek = date('Y-m-d', strtotime('Monday last week'));
//echo $lastweek;
$mquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Matthew') AND (nowdate >= '$thisweek')";
$mresult = mysqli_query($con, $mquery);
$mdata = mysqli_fetch_assoc($mresult);
$mcount = $mdata['total'];
$rquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Roisin') AND (nowdate >= '$thisweek')";
$rresult = mysqli_query($con, $rquery);
$rdata = mysqli_fetch_assoc($rresult);
$rcount = $rdata['total'];
$mlquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Matthew') AND (nowdate >= '$lastweek') AND (nowdate < $thisweek)";
$mlresult = mysqli_query($con, $mlquery);
$mldata = mysqli_fetch_assoc($mlresult);
$mlcount = $mldata['total'];
$rlquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Roisin') AND (nowdate >= '$lastweek') AND (nowdate < $thisweek)";
$rlresult = mysqli_query($con, $rlquery);
$rldata = mysqli_fetch_assoc($rlresult);
$rlcount = $rldata['total'];
if ($mlcount > $rlcount) {
$lcount = "Matthew";
$first = $mlcount;
$second = $rlcount;
}
else {
$lcount = "Roisin";
$first = $rlcount;
$second = $mlcount;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title>Yellow Car</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css">
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="favicon.ico">
</head>
<body>
<div class="container">
<div class="pure-g-r">
<div class="pure-u-1 text-center">
<h1>Yellow Car!</h1>
</div>
</div>
<div class="pure-g">
<div class="pure-u-1-2 padded text-center">
<form class="pure-form" method="post" action="matt-add.php">
<button type="submit" class="pure-button button-car pure-input-1">Matthew</button>
</form>
<p>
This Week: <strong class="blue-text"><?php echo $mcount; ?></strong>
</p>
</div>
<div class="pure-u-1-2 padded text-center">
<form class="pure-form" method="post" action="rosh-add.php">
<button type="submit" class="pure-button button-car pure-input-1">Roisin</button>
</form>
<p>
This Week: <strong class="blue-text"><?php echo $rcount; ?></strong>
</p>
</div>
</div>
<div class="pure-g-r">
<div class="pure-u-1 text-center">
<h2>
<?php
if ($first == $second) {
?>
Last week was a <span class="blue-text">draw</span>! (<?php echo $first . "-" . $second; ?>)
<?php
}
else {
?>
Last week's winner was: <span class="blue-text"><?php echo $lcount; ?></span>! (<?php echo $first . "-" . $second; ?>)
<?
}
?>
</h2>
</div>
</div>
</div>
</body>
</html>
数据库很简单;一个记录日期和名称的表。添加查询:
INSERT INTO records (who, nowdate) VALUES ('$who', NOW())
我试着回应1月2日星期日的日期,但它告诉我本周是第9天,上周是第2天,正好是7天。
如果有人可以告知我的代码中的任何错误,那将非常感激。
答案 0 :(得分:1)
您需要在以下两个查询中单引号$thisweek
...
$mlquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Matthew') AND (nowdate >= '$lastweek') AND (nowdate < '$thisweek')";
$mlresult = mysqli_query($con, $mlquery);
$mldata = mysqli_fetch_assoc($mlresult);
$mlcount = $mldata['total'];
$rlquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Roisin') AND (nowdate >= '$lastweek') AND (nowdate < '$thisweek')";