<?php

error_reporting(E_ALL ^ E_DEPRECATED);

$con = mysqli_connect("localhost","labandroid_absencam1","absencam1Pwd","labandroid_absencam1");

/*
$SQL1="SELECT * FROM data_logs WHERE idrfid=".$_GET['Data']." AND tgl=CURDATE() ORDER BY time_update DESC LIMIT 1";    
echo $SQL1;
$qry_ksr1=mysqli_query($con,$SQL1); 
$row_ksr1=mysqli_fetch_array($qry_ksr1);
$row_cnt1 = mysqli_num_rows($qry_ksr1);
if($row_cnt1)
{
    $SQL2="UPDATE data_logs SET rekap_keluar = CURTIME(),status2=1 WHERE id =".$row_ksr1['id'].""; 
    mysqli_query($con,$SQL2); 
}else{
    $qry="INSERT INTO data_logs (tgl,idrfid,status1,rekap_masuk) VALUES (CURRENT_DATE(),".$_GET['Data'].", '1',CURTIME())";    
    mysqli_query($con,$qry);
} 

$sql="UPDATE rfid_code SET rfid_code = ".$_GET['Data']."";
mysqli_query($con,$sql);
*/


/*
$SQL1="SELECT * FROM `data_users` WHERE idcode=".$_GET['Data']."";
$qry_ksr1=mysqli_query($con,$SQL1); 
$row_ksr1=mysqli_fetch_array($qry_ksr1);
$row_cnt1 = mysqli_num_rows($qry_ksr1);
if($row_cnt1)
{
    echo $row_ksr1['nama'];
    $qry="INSERT INTO data_logs (username1,tgl,idrfid,status1) VALUES ('".$row_ksr1['nama']."',CURRENT_DATE(),".$_GET['Data'].", '1')";  
    mysqli_query($con,$qry);    
}else{
    
    mysqli_query($con,"TRUNCATE rfid_code");    
   
    $sql="INSERT INTO rfid_code (rfid_code) VALUES (".$_GET['Data'].")"; 
    mysqli_query($con,$sql);    
}

*/

//echo "ok aja";

$SQL1="SELECT * FROM `data_users` WHERE idcode=".$_GET['Data']."";
$qry_ksr1=mysqli_query($con,$SQL1); 
$row_ksr1=mysqli_fetch_array($qry_ksr1);
$row_cnt1 = mysqli_num_rows($qry_ksr1);
if($row_cnt1)
{
    $SQL1="SELECT * FROM data_logs WHERE idrfid=".$_GET['Data']." AND tgl=CURDATE() ORDER BY time_update DESC LIMIT 1";    
    $qry_ksr2=mysqli_query($con,$SQL1); 
    $row_ksr2=mysqli_fetch_array($qry_ksr2);
    $row_cnt2 = mysqli_num_rows($qry_ksr2);
    if($row_cnt2)
    {
        echo $row_ksr1['nama'];
        $SQL2="UPDATE data_logs SET tgl2 = NOW(),status2=1 WHERE id =".$row_ksr2['id'].""; 
        mysqli_query($con,$SQL2); 
    }else{
         echo $row_ksr1['nama'];
        $qry="INSERT INTO data_logs (username1,tgl,idrfid,status1,tgl1) VALUES ('".$row_ksr1['nama']."',CURRENT_DATE(),".$_GET['Data'].", '1',NOW())";    
        mysqli_query($con,$qry);
    } 
}else{
    mysqli_query($con,"TRUNCATE rfid_code");    
    $sql="INSERT INTO rfid_code (rfid_code) VALUES (".$_GET['Data'].")"; 
    mysqli_query($con,$sql); 
}

?>