有人要求修复几年前使用自定义 CMS 创建的数据库中的错误。我仍然主要是前端人员,但我越来越多地进入这类项目并尝试学习,现在显示的错误是
Fatal error: Duplicate entry '2012-12-10-1' for key 'PRIMARY' INSERT INTO stats SET pageID = '1', date = NOW(), visits = 1, views = 1 in /home/site/domains/domain.nl/public_html/cms/inc/functions.inc.php on line 10
奇怪的是日期似乎有两个月。我了解正在将统计信息添加到统计数据库中,并且它与另一个重复。我只是无法使用 PHPMyAdmin 在表中找到此条目。我试着修理桌子,但没有帮助。错误是由functions.inc.php中的这个函数触发的:
function executeQuery($query) {
global $website;
static $count;
benchmark("mysql", "start");
if ($query == "count") {
return $count;
}
$count++;
$result = mysql_query($query, $website->DBConnection) or trigger_error("\n" . mysql_error() . "\n" . $query, E_USER_ERROR);
$time += (microtime(true) - $tmp);
benchmark("mysql", "stop");
return $result;
}
Page.class.php 上的函数是:
public function updateStats() {
$result = executeQuery("SELECT count(*) FROM stats WHERE pageID = '" . $this->id . "' AND date = NOW()");
$firstTime = mysql_result($result, 0) == 0;
if ($firstTime) { // First time today someone visit this page
executeQuery("INSERT INTO stats SET pageID = '" . $this->id . "', date = NOW(), visits = 1, views = 1");
$_SESSION["stats"][$this->id] = true;
} else {
if (!$_SESSION["stats"][$this->id]) { // First time today this person visit this page
executeQuery("UPDATE stats SET visits = visits + 1, views = views + 1 WHERE pageID = '" . $this->id . "' AND date = NOW()");
$_SESSION["stats"][$this->id] = true;
} else { // Second or more time someone visit this page.
executeQuery("UPDATE stats SET views = views + 1 WHERE pageID = '" . $this->id . "' AND date = NOW()");
}
}
}
由于该错误,该站点似乎已关闭。我没有看到在 2012-10-04 之后输入的任何统计信息。如何创建这样的日期,称为 DUPLICATE,我该如何解决这个问题?任何帮助表示赞赏。
根据@Roman Newaza 的要求
SHOW CREATE TABLE stats
stats CREATE TABLE `stats` (
`date` date NOT NULL DEFAULT '0000-00-00',
`views` int(11) NOT NULL DEFAULT '0',
`visits` int(11) NOT NULL DEFAULT '0',
`pageID` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`date`,`pageID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1