Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:17:04+00:00 2026-06-11T20:17:04+00:00

This is a follow-up question to this one: Check for specific integer in a

  • 0

This is a follow-up question to this one: Check for specific integer in a row WHERE user = $name

I want a user to be able to comment on my site for exactly five times a day. After this five times, the user has to wait 24 hours.
In order to accomplish that I raise a counter in my MYSQL database, right next to the user.
So where the name of the user is, there is where the counter gets raised. When it reaches 5 it should stop counting and reset after 24 hours. In order to check the time I use a timestamp. I check if the timestamp is older than 24 hours. If that is the case, the counter gets reseted (-5) and the user can comment again. In order to do that, I use the following code, however it never stops at five, my guess is that my comparison is wrong somehow:

$counter = mysql_query("SELECT FROM table VALUES CommentCounterReset WHERE Name = '$name'");

if(!isset($_SESSION['ts'])); {
    $_SESSION['ts'] = time();
}

if  ($counter >= 5) { 
    if (time() - $_SESSION['ts'] <= 60*60*24){
        echo "You already wrote five comments.";
    }
    else {
        $sql = "UPDATE table SET CommentCounterReset = CommentCounterReset-5 WHERE Name = '$name'";   
    }     
}  
else {
 $sql = "UPDATE table SET CommentCounterReset = CommentCounterReset+1 WHERE Name = '$name'";
    echo "Your comment has been added.";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:17:05+00:00Added an answer on June 11, 2026 at 8:17 pm

    You should let him add comments 5 times a day, not after 24 hours:

    $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.";
        }
    }
    

    for mysql_* but I’m highly discouraging it!

    $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.";
        }
    }
    

    my 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 ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-on question from the one I asked here . Can constraints
This is a follow-up question to this one . Consider this example: #include <iostream>
This is like a follow-up question to this one . Basically what I'm doing
Just a follow up question to this one here => link Is it possible
I have a follow-up question to this one . I created a new form,
I have a follow up question to this one . Now that I have
This is really a follow on question to a previous one , where I
This question is a follow on from this one ... I am binding to
This is a follow-up question to this one .. Thanks to Nick Lockwood, I
I'm aware of this question, and it's follow-up, and also this one, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.