Jump to content

Recommended Posts

i'm trying to do you have to wait 30 seconds between posts. this isn't working though. when i test i post under 30 seconds between post and it keeps echo "worked";. what did i do wrong?

 

mysql_connect("localhost", "Master", "pword");
mysql_select_db("db");
$visitor = $_SERVER['REMOTE_ADDR']; 
$lastpost = mysql_query("SELECT * FROM Stacks WHERE ip='$visitor' ORDER BY posted DESC")or die (mysql_error());

while($latest = mysql_fetch_assoc($lastpost)){

$thelatest= strtotime($latest['posted']);
}



echo (time() - $thelatest);
if ((time() - $thelatest) >  30){
echo "worked";

} else

{

echo "you must wait 30 seconds";

}

Link to comment
https://forums.phpfreaks.com/topic/207094-help-with-time-if-statement/
Share on other sites

$lastpost = mysql_query("SELECT * FROM Stacks WHERE ip='$visitor' ORDER BY posted DESC")or die (mysql_error());

while($latest = mysql_fetch_assoc($lastpost)){

$thelatest= strtotime($latest['posted']);
}

 

That query is retrieving EVERY post from that user. The ORDER BY posted DESC means you get the newest one first and the oldest one last. Then you loop through the entire resultset calculating $thelatest. This means at the end of the while loop, $thelatest contains the date of the user's FIRST post. 

 

// 1) ADD A LIMIT TO THE QUERY
$lastpost = mysql_query("SELECT * FROM Stacks WHERE ip='$visitor' ORDER BY posted DESC LIMIT 1")or die (mysql_error());

// 2) GET RID OF THE WHILE LOOP, YOU DO NOT NEED TO WALK THROUGH THE RESULTS
$latest = mysql_fetch_assoc($lastpost));

$thelatest= strtotime($latest['posted']);

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.