problem in mysqli query - timestamp
i have a problem with my query.
fields in the database (userid, code, timestamp
)
I want to verify in this code if exists an userid and the respective code but that was inserted less than one day, otherwise show a message "link expired"
My code works well without this line TIMESTAMPDIFF(DAY, CURTIME(), timestamp) < 1")
.
At the moment nothing is returned with the timestamp condition
"select userid, code from password_reset where userid=? and code=? and TIMESTAMPDIFF(DAY, CURTIME(), timestamp) < 1"
There is some problem in this code? Basically my idea is: if the timestamp has more t开发者_StackOverflow社区han a day, return an error to the user.
my script (the problem is only in the timestamp condition in the query)
if (checkBd ($sql, $db, $user, $codePass)){
$user = (int)mysqli_real_escape_string($db, $userid);
$codePass = mysqli_real_escape_string($db, $_GET['code']);
($sql = $db->prepare("select userid, code from password_reset where userid=? and code=? and TIMESTAMPDIFF(DAY, CURTIME(), timestamp) < 1"));
$sql->bind_param('ss', $user, $codePass);
$sql->execute();
$sql->bind_result($user, $codePass);
if ($sql->fetch()) {
$_SESSION['u_name']= sha1($user);
header("location: updatePass.php");
return true;
}
}
$sql->close();
$db->close();
You want to pass a DATETIME
value, not just a TIME
, use NOW()
not CURTIME()
. (If you want to ignore the time of day, then use CURDATE()
.
Also, reverse the argument order, as the function does datetime2 - datetime1
.
See the documentation for more details.
TIMESTAMPDIFF(DAY, `timestamp`, NOW()) < 1
Using a reserved word for your column name won't help either.
CURTIME()
returns curent time (without date). I suppose the date used there by TIMESTAMPDIFF
is 'min_date', try using instead CURDATE()
精彩评论