Someone asked to fix an error in a database created a few years ago using a custom CMS. I am still mainly a frontend guy but I am getting more and more into these kind of projects and try to learn, The error shown now is
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
What is odd is the date which seems to have two months in it. I understand a stat was being added to the stat database and that it was a duplicate of another. I just cannot find this entry in the table using PHPMyAdmin. I tried repairing the table, but that did not help. The error is fired off by this function in 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;
}
The function on the Page.class.php is:
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()");
}
}
}
Since that error the site seems to have been down.. I do not see any stats entered after 2012-10-04 . How can such a date be created, called DUPLICATE and how can I remedy this? Any help is appreciated.
As requested by @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
When you have a compound primary key, the duplicate entry error message are formatted with a dash separator, like
X-Y, where X is the value in one column and Y is the value in a second column.It’s just a coincidence in this case that the default date format also has dashes in it.
So you are setting the
datecolumn to NOW() which is ‘2012-12-10’ and a second integer column (I’m guessingpageID) with value ‘1’. The combination of these two values is already in the table, and that’s what’s violating the primary key. I infer from this that your table definition is something like:As for why you don’t see any data after 2012-12-04, I can guess that the duplicate key error caused the transaction to be rolled back, so both the entry that caused the error and the row that it conflicted with were undone.