我曾尝试使用PHP和SQL创建一个考勤系统。插入部分运行良好,但我想检查出勤记录的数据库是否已经存储。如果已存储,则回显已存储,否则将时间和日期插入数据库表。但是该守则不起作用。
每次我单击Intime按钮,在每个按钮上单击存储在数据库中的时间和日期。
代码如下:
$eid=$_SESSION['emplogin'];
$sql = "SELECT * from tbl_employees where EmailId=:eid";
$query = $dbh -> prepare($sql);
$query->bindParam(':eid',$eid,PDO::PARAM_STR);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$id = $result->EmpId;
if(isset($_POST['intime']))
{
date_default_timezone_set('Asia/Kolkata');
$sql = "SELECT date FROM tbl_in_time_attendance where EmpId = :eid ORDER BY tbl_in_time_attendance.date DESC LIMIT 1";
$query = $dbh->prepare($sql);
$query->bindParam(':eid',$eid,PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_OBJ);
$dbdate = date('Y-m-d', strtotime($result->date));
$date = date('Y-m-d');
if($dbdate != $date)
{
$sql = "INSERT INTO tbl_in_time_attendance (EmpId)VALUES (:id)";
$query = $dbh->prepare($sql);
$query->bindParam(':id',$id,PDO::PARAM_STR);
$query->execute();
}
else
{
$message= "In Time Of today is already stored";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
答案 0 :(得分:0)
您正在将数据库日期时间与当前日期时间进行比较,并且永远不会相同,因此您必须比较日期
尝试下面的代码,它应该可以按预期工作,
if(isset($_POST['intime']))
{
date_default_timezone_set('Asia/Kolkata');
$sql = "SELECT date FROM tbl_in_time_attendance where EmpId = :eid ORDER BY tbl_in_time_attendance.date DESC LIMIT 1";
$query = $dbh->prepare($sql);
$query->bindParam(':eid',$eid,PDO::PARAM_STR);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_OBJ);
$dbdate = date('Y-m-d', strtotime($result->date));
$date = date('Y-m-d');
if($dbdate != $date) {
$sql = "INSERT INTO tbl_in_time_attendance (EmpId)VALUES (:id)";
$query = $dbh->prepare($sql);
$query->bindParam(':id',$id,PDO::PARAM_STR);
$query->execute();
} else {
$message= "In Time Of today is already stored";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}