What are the best practices for recording unauthorized access attempts in a MySQL table using PHP?

Unauthorized access attempts in a MySQL table should be recorded to track potential security threats and take appropriate actions. To achieve this, you can create a separate table to log these attempts and insert relevant information such as the IP address, timestamp, and any other relevant details. This will help in identifying patterns of unauthorized access and enhancing the security of your application.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check for unauthorized access
if($unauthorized_access) {
    // Insert unauthorized access attempt into log table
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $timestamp = date('Y-m-d H:i:s');
    $query = "INSERT INTO access_log (ip_address, timestamp) VALUES ('$ip_address', '$timestamp')";
    $mysqli->query($query);
}