What are the potential pitfalls of not properly escaping user inputs in PHP when interacting with a MySQL database?

Not properly escaping user inputs in PHP when interacting with a MySQL database can lead to SQL injection attacks, where malicious users can manipulate the SQL queries to access, modify, or delete data. To prevent this, always sanitize and escape user inputs before using them in SQL queries.

// Example of properly escaping user inputs in PHP when interacting with a MySQL database
$user_input = $_POST['user_input']; // Assuming this is the user input from a form

// Escape the user input using mysqli_real_escape_string
$escaped_user_input = mysqli_real_escape_string($connection, $user_input);

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

// Remember to handle errors and exceptions appropriately