What are the potential security risks associated with not properly handling special characters in SQL queries in PHP?

Improperly handling special characters in SQL queries in PHP can lead to SQL injection attacks, where malicious users can manipulate the query to access or modify sensitive data. To prevent this, it is important to properly escape special characters in user input before using them in SQL queries.

// Example of properly handling special characters in SQL queries in PHP

// Get user input
$user_input = $_POST['user_input'];

// Escape special characters
$escaped_input = mysqli_real_escape_string($connection, $user_input);

// Use the escaped input in the SQL query
$query = "SELECT * FROM users WHERE username = '$escaped_input'";
$result = mysqli_query($connection, $query);