如何检查记录是否已注册

时间:2018-06-04 10:45:20

标签: php mysql

我想检查一个id是否在用户表中注册在MySQL中,如果它在用户表中注册,则输入RFID并且如果不是echo无效id。到目前为止我已经这样做了,没有任何检查就进入了。这是tbl_attendance,而ids在tbl_user中注册。我从Arduino那里得到了自己的身份。

<?php
include ('connection.php');
$sql_insert = "INSERT INTO tbl_attendance (rfid_uid) VALUES ('".$_GET["rfid_uid"]."')";
if(mysqli_query($con,$sql_insert))
{
mysqli_close($con);
}

?>

1 个答案:

答案 0 :(得分:-1)

此代码应该按照您的要求进行操作。您应该考虑使用预准备语句(例如PDO),因为不推荐使用mysqli库。

<?php

    //Include the mysql connection to the database
    include ('connection.php');

    //Find the existing RFID rows
    $result = mysqli_query($con, "SELECT id FROM tbl_user WHERE rfid_uid = '". $_GET["rfid_uid"] ."'");

    //Count the number of rows
    $count = mysqli_num_rows($result);

    if( $count == 1 ){ //If the user exists in the tbl_usr
        //Build the table insert query
        $sql_insert = "INSERT INTO tbl_attendance (rfid_uid) VALUES ('".$_GET["rfid_uid"]."')";

        //Execute that query
        if( mysqli_query($con, $sql_insert) ){
            echo "Attendance registered successfully!";    //Success message
        } else {
            echo "Attendance failed to register!";         //Failure message
        }
    } else {
        //Display the "Invalid ID Message"
        echo "Invalid id";
    }

    //Close the mysql connection object
    mysqli_close($con);

?>
相关问题