我有一个用户登录的系统。每次特定用户登录时,我都会使用TIMESTAMP:将登录日期时间存储在MySQL数据库中
数据库中的每条记录都有一个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天。因此,我希望用户每天只能登录一次。我仍在寻找正确的方式来实现对用户的限制,让他们每天登录一次。
谢谢!
// 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());
}