Search code examples
phpmysqlarduinorfid

I want to mark attendance of a student once per day in my database


I want to scan RFID cards from Arduino and it should mark attendance once per day not whenever it scans the card.

This is the PHP code for marking attendance:

public function getRFIDAttendanceApi(){
    $rfid_uid = trim($_REQUEST['uid']);
    $sql = "select count(*)as tot from tbl_users where rfid_uid='$rfid_uid'";

    $data = $this->getData($sql);
    if(count($data)>0 && $data[0]['tot']>0){
        $sql = "insert into tbl_attendance set rfid_uid='$rfid_uid'";
        $flag = $this->conn->query($sql);
        if($flag){
            echo "Attendance marked successfully!";
            die;
        } else {
            echo "Sorry! Something unexpected happened!";
            die;    
        }
    } else {
        echo "invalid access";die;
    }

This is the table in mysql for attendance with repeating marking

| att_id | rfid_uid | punch_timestamp     | date       | time     |
+--------+----------+---------------------+------------+----------+
|      1 | 85988145 | 2018-04-21 10:48:51 | 2018-04-21 | 10:48:51 |
|      2 | 85988145 | 2018-04-21 10:48:52 | 2018-04-21 | 10:48:52 |

How to make it once per day? Thanks.


Solution

  • Just add a unique contraint to the attendance table on the rfid_uid and date.

    ALTER TABLE tbl_attendance ADD CONSTRAINT UK_ATTENTANCE UNIQUE(rfid_uid, `date`);
    

    This way just the first successful punch on in a day from the card will be inserted in the database.