我有一个用户登录它的系统。每次特定用户登录时,我都会使用TIMESTAMP:
将登录日期时间存储在MySQL数据库中db中的每条记录都有一个user_id和login_date,它存储在我的att_log表中。 但是,我想确保用户每天只能根据日期登录一次。到目前为止我的代码
<?php
include('conn.php');
if (isset($_POST['submit'])) {
$UserName=$_POST['id_emp'];
$Password=$_POST['password'];
$result=mysql_query("select * from employee where id_emp='$UserName' and password='$Password'" )or die (mysql_error());
$count=mysql_num_rows($result);
$row=mysql_fetch_array($result);
if ($count > 0){
$_SESSION['emp_id']=$row['emp_id'];
mysql_query("insert into att_log (username,login_date,id)values('$UserName',NOW(),".$row['emp_id'].")")or die(mysql_error());
?>
如果user_id在相同日期登录,如何在将其插入att_log表之前拒绝,如何编写正确的语句来检查案例。 使用当前代码,它将在同一天不断地连续插入同一个用户,只是时间不同。
我想在特定日期过滤掉特定用户,因为我确实计算了他们在月末的频率,例如
id_attlog username logindate id
1 2010 2015-06-20 19:08:19 33
2 2011 2015-06-20 19:08:19 34
3 2011 2015-06-21 19:08:19 34
4 2011 2015-06-22 19:08:19 34
User ID Day of Attendance
33 3
34 1
用户ID 34已登录3天,用户33仅登录1天。这就是为什么我希望用户每天只能登录一次。我仍在寻找正确的方法来实现对用户的限制,让他们每天登录一次。
谢谢!
答案 0 :(得分:0)
// Compare username/password and check last login date is less than today
$result = mysql_query('select a.*, max(b.login_date) as login_date from employee AS a LEFT JOIN att_log AS b ON a.emp_id = b.id where a.emp_id = "' . mysql_real_escape_string($UserName) . '" and a.password = "' . mysql_real_escape_string($Password) . '" and login_date < DATE(NOW()) GROUP BY a.emp_id' ) or die (mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
mysql_query('insert into att_log (username, login_date, id) values("' . mysql_real_escape_string($UserName) . '", NOW(), "' . mysql_real_escape_string($row['emp_id']) . '"') or die(mysql_error());
} else {
echo 'Access denied';
}
或:
// Compare username/password
$result = mysql_query('select a.*, max(b.login_date) as login_date from employee AS a INNER JOIN att_log AS b ON a.emp_id = b.id where a.emp_id = "' . mysql_real_escape_string($UserName) . '" and a.password = "' . mysql_real_escape_string($Password) . '" GROUP BY a.emp_id' ) or die (mysql_error());
$row = mysql_fetch_array($result);
// If last login date is greater or equal than the start of today, show access denied message
if (strtotime($row['login_date']) >= strtotime(date('Y-m-d 00:00:00')) {
echo 'Sorry you have already logged in today';
} else {
mysql_query('insert into att_log (username, login_date, id) values("' . mysql_real_escape_string($UserName) . '", NOW(), "' . mysql_real_escape_string($row['emp_id']) . '"') or die(mysql_error());
}