您应该让他每天添加 5 次评论,而不是 24 小时后:
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$stmt = $db->prepare("SELECT `Name`, `timestamp`, `CommentCounterReset` FROM `table` WHERE `Name`=:name");
$stmt->execute(array(':name' => $_GET['name']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$lastComment = $row['timestamp']; // timestamp from database and not from $_SESSION because $_SESSION get's destroyed.
$countComments = $row['CommentCounterReset']; // number of comments added
$row_count = $stmt->rowCount();
if(!$row_count){
$stmt = $db->prepare("INSERT INTO `table` VALUES(0, :name, :timestamp, 1)");
$stmt->execute(array(':name' => $_GET['name'], ':timestamp' => date('U')));
echo "Your comment has been added.";
} else {
if($countComments > 4 && date('d') == date('d', $lastComment)){ // if number of comments are greater than 4 and is same day
echo "You already wrote five comments today.";
} else {
if(date('d') != date('d', $lastComment)){ // if there are different days, reset the counter to 1
$stmt = $db->prepare("UPDATE `table` SET `CommentCounterReset` = 1, `timestamp`=:timestamp WHERE `Name`=:name");
$stmt->execute(array(':name' => $_GET['name'], ':timestamp' => date('U')));
} else { // if it's same day increase counter with 1
$stmt = $db->prepare("UPDATE `table` SET `CommentCounterReset` = `CommentCounterReset`+1 WHERE `Name`=:name");
$stmt->execute(array(':name' => $_GET['name']));
}
echo "Your comment has been added.";
}
}
因为mysql_*
但我非常不鼓励它!
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $link);
$stmt = mysql_query("SELECT `Name`, `timestamp`, `CommentCounterReset` FROM `table` WHERE `Name`='".mysql_real_escape_string($_GET['name'])."'");
$num = mysql_num_rows($stmt);
if(!$num){
$stmt = mysql_query("INSERT INTO `table` VALUES(0, '".mysql_real_escape_string($_GET['name'])."', '".date('U')."', 1)");
echo "Your comment has been added.";
} else {
$row = mysql_fetch_assoc($stmt);
$lastComment = $row['timestamp']; // timestamp from database and not from $_SESSION because $_SESSION get's destroyed.
$countComments = $row['CommentCounterReset']; // number of comments added
if($countComments > 4 && date('d') == date('d', $lastComment)){ // if number of comments are greater than 4 and is same day
echo "You already wrote five comments today.";
} else {
if(date('d') != date('d', $lastComment)){ // if there are different days, reset the counter to 1
$stmt = mysql_query("UPDATE `table` SET `CommentCounterReset` = 1, `timestamp`='".date('U')."' WHERE `Name`='".mysql_real_escape_string($_GET['name'])."'");
} else { // if it's same day increase counter with 1
$stmt = mysql_query("UPDATE `table` SET `CommentCounterReset` = `CommentCounterReset`+1 WHERE `Name`='".mysql_real_escape_string($_GET['name'])."'");
}
echo "Your comment has been added.";
}
}
我的table
:
CREATE TABLE `table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(30) NOT NULL,
`timestamp` int(11) NOT NULL,
`CommentCounterReset` int(11) NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;