我使用以下代码来确定用户是否已登录,我已设置字段' first_sign_in'在mysql表中为0,但是我仍然收到已经登录的回应,当天它应该返回并且没有在当天开始时登录'
有人可以帮我解决我在这里出错的地方。
$time = date('h:i:s', time());
$checkifstaffexists = mysql_query("SELECT user_id from staff WHERE pin = 3012");
if (!$checkifstaffexists) {
die('Failed.');
}
if (mysql_num_rows($checkifstaffexists) > 0) {
$checkfirstsignin = mysql_query("SELECT first_sign_in from staff WHERE pin = 3012");
if ($checkfirstsignin == 0) {
echo 'not signed in for start of day</br>';
$checksignintime = mysql_query("SELECT " . date("d") . " " . "_start from staff WHERE pin = 3012");
if($checksignintime > $time) {
echo 'user is late';
$addtolatetable = mysql_query("INSERT INTO lates (user_id, date_time) SELECT user_id, '2014-05-15 12:00:00' from staff WHERE pin = 3012");
//$signuserin = mysql_query(" ");
$changestatustoin = mysql_query("UPDATE staff SET status=1 WHERE pin = 3012");
//redirect
} else {
echo 'user is not late';
//$signuserin = mysql_query(" ")
$changestatustoin = mysql_query("UPDATE staff SET status=1 WHERE pin = 3012");
//redirect
}
} else {
echo 'already signed in for start of day</br>';
$checkifuserisinourout = mysql_query("SELECT status from staff WHERE pin = 3012");
if ($checkifuserisinourout == 0) {
echo 'user is not signed in so we will sign you in';
//$signuserin = mysql_query(" ");
$changestatustoin = mysql_query("UPDATE staff SET status=1 WHERE pin = 3012");
//redirect
} else {
echo 'user is signed in so we will sign you out';
//$signuserout = mysql_query(" ");
$changestatustoout = mysql_query("UPDATE `staff` SET status=0 WHERE pin = '3012'");
//redirect
}
}
} else {
//The user cannot be found
echo 'User doesn\'t exist.';
}
答案 0 :(得分:2)
行
$checkfirstsignin = mysql_query("SELECT first_sign_in from staff WHERE pin = 3012");
您将获得必须用于获取数据的资源,例如:
$row = mysql_fetch_assoc($checkfirstsignin);
并使用此数组($ row),您可以进一步工作。
请查看mysql_query的联机帮助页以获取进一步阅读...
因为这将一直发布:不推荐使用mysql_ *方法,请使用mysqli或pdo。
答案 1 :(得分:0)
if ($checkfirstsignin == 0)
如果查询成功,即使没有匹配的结果,也总是等于true。 你需要使用mysql_fetch_row或mysql_fetch_array来做到这一点。
while($row = mysql_fetch_assoc($checkfirstsignin)){
if($row['first_sign_in']==0){
//do something
}
}
答案 2 :(得分:0)
写下这个var_dump( $checkfirstsignin );
此行$checkfirstsignin = mysql_query("SELECT first_sign_in from staff WHERE pin = 3012");
并且您将看到返回的结果是一个数组,因此如果检查,则下一个结果总是为假。