MDanz Posted September 15, 2009 Share Posted September 15, 2009 every record in the table has a timestamp $time= $row['posted']; how do i say in php if $time less than 30 seconds compared to present time where username=$username? Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/ Share on other sites More sharing options...
MartinGr Posted September 15, 2009 Share Posted September 15, 2009 I don't know what you exactly mean by username=$username, but if ( ($time > (time()-30)) && ($username == 'sth') ) {} The code gets executed, if the value of $time is less than 30 seconds compared to present time and username equals the one you want. Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/#findComment-918858 Share on other sites More sharing options...
MDanz Posted September 15, 2009 Author Share Posted September 15, 2009 its not working.. it just echos "success!" even when i just posted a second ago. The only time i should see "success" is after 50 seconds.. Where did i go wrong? $username = $_SESSION['username']; mysql_connect("localhost", "Master", "password"); mysql_select_db("dbk"); $result = mysql_fetch_assoc(mysql_query("SELECT * FROM Stacks"))or die (mysql_error()); $user = $result['username']; $time = $result['posted']; if ( ($time > (time()-50)) && ($username == $user) ) { echo "<font color=white>You must wait 30 seconds between submits</font>"; } else { echo "Success!"; } Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/#findComment-918884 Share on other sites More sharing options...
KevinM1 Posted September 15, 2009 Share Posted September 15, 2009 Think it out: If there's less than 50 seconds difference between the current time and the time in the db, deny access - if ((time() - $time < 50)) { //posted too soon } Make sure that $time is a Unix timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/#findComment-918890 Share on other sites More sharing options...
grissom Posted September 15, 2009 Share Posted September 15, 2009 Add this debugging line to your code just before your IF statement echo "The value of my variable $time is : ".$time." The value of time() is : ".time()." The difference between the two is : ".$time-time(); Let me know what you get. Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/#findComment-918892 Share on other sites More sharing options...
PFMaBiSmAd Posted September 15, 2009 Share Posted September 15, 2009 It's also probably not working because you are querying the database for everything in it, then just using whatever values are in the first row retrieved. Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/#findComment-918894 Share on other sites More sharing options...
MDanz Posted September 15, 2009 Author Share Posted September 15, 2009 Add this debugging line to your code just before your IF statement echo "The value of my variable $time is : ".$time." The value of time() is : ".time()." The difference between the two is : ".$time-time(); Let me know what you get. i tried that and i get this -1253024435 it doesn't even say the words just -1253024435 still not working.. <?php session_start(); include("connect.php"); $username = $_SESSION['username']; mysql_connect("localhost", "ustackc1_Master", "KOWstHbK"); mysql_select_db("ustackc1_Ustack"); $result = mysql_fetch_assoc(mysql_query("SELECT * FROM Stacks WHERE username='$username'"))or die (mysql_error()); $user = $result['username']; $time = $result['posted']; if ((time() - $time < 50)) { echo "<font color=white>You must wait 30 seconds between submits</font>"; } else { echo "success"; } Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/#findComment-918909 Share on other sites More sharing options...
PFMaBiSmAd Posted September 15, 2009 Share Posted September 15, 2009 Here is a troubleshooting hint: when a comparison fails, echo out the value(s) being used in the comparison so that you know exactly what it is. Are you even sure that there is a row in Stacks for the username? Your code is not checking if the query worked or failed and that there is a row in the result set. What do you get when you add the following two lines of code immediately after your first opening <?php tag - ini_set("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/#findComment-918916 Share on other sites More sharing options...
MDanz Posted September 15, 2009 Author Share Posted September 15, 2009 still get the same page the query is working, there is a row in Stacks for that username.. each row has a timestamp when i do this echo "The value of my variable $time is : ".$time." The value of time() is : ".time()." The difference between the two is : ".$time-time(); it displays "-1253026593" it doesn't even show the text in the echo just "-1253026593" when i echo $time i get this 2009-08-31 23:41:52 they are in two different formats? Quote Link to comment https://forums.phpfreaks.com/topic/174307-help-with-if-statement/#findComment-918939 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.