I am trying to do a basic MySQL query. But when I run my code, it doesn't properly check if a record with that name already exists.
Here is my code:
            $loggedinUserID = $_SESSION['user'];
            $loggedinUserName = $_SESSION['name'];
            //check if user is logged in
            if(empty($loggedinUserID)) {header('Location: ../');}
            $servername = "localhost";
            $username = "{hidden}";
            $password = "{hidden}";
            // Create connection
            $conn = mysqli_connect($servername, $username, $password, '{hidden}');
            // Check connection
            if (!$conn) {
               die("Connection failed: " . mysqli_connect_error());
            }
            $sql = "SELECT accessId FROM users WHERE userId=$loggedinUserID";
            $final = mysqli_query($conn, $sql);
            $result = mysqli_fetch_assoc($final);
            echo $result;
            if(empty($result)) {
                $sql2 = "INSERT INTO users (`userId`, `userName`, `accessId`) VALUES ('$loggedinUserID', '$loggedinUserName', '0')";
                if(mysqli_query($conn, $sql2)) {
                    echo "Your account was created successfully, however you have not been permitted access. Please contact system admins to be granted access.";
                }
                else {
                    echo "An error occured when creating your account. --->" . mysqli_error($conn);
                }
            }
When doing this it returns the error An error occured when creating your account. --->Duplicate entry '2147483647' for key 'userId'.
I know I have a record for this user already... But I do not know why it isn't detecting that record. Please help.
 
     
     
    