What are the potential security risks associated with using unescaped user input in MySQL queries in PHP?

Using unescaped user input in MySQL queries in PHP can lead to SQL injection attacks, where malicious users can manipulate the query to execute unintended actions on the database. To prevent this, it is important to properly escape user input before using it in a query. This can be done using functions like mysqli_real_escape_string() or prepared statements.

// Using mysqli_real_escape_string to escape user input before using it in a query
$connection = mysqli_connect("localhost", "username", "password", "database");

$user_input = mysqli_real_escape_string($connection, $_POST['user_input']);

$query = "SELECT * FROM users WHERE username = '$user_input'";
$result = mysqli_query($connection, $query);

// Rest of the code to handle the query result