What are the potential pitfalls of not properly escaping user input in PHP MySQL queries?

Not properly escaping user input in PHP MySQL queries can lead to SQL injection attacks, where malicious users can manipulate the query to execute unauthorized actions on the database. To prevent this, always sanitize and escape user input before using it in MySQL queries using functions like mysqli_real_escape_string() or prepared statements.

// Using mysqli_real_escape_string() to escape user input
$user_input = $_POST['user_input'];
$escaped_input = mysqli_real_escape_string($connection, $user_input);

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