What are the potential security risks associated with directly inserting user input into a MySQL query in PHP?

Directly inserting user input into a MySQL query in PHP can lead to SQL injection attacks, where malicious users can manipulate the query to access, modify, or delete sensitive data. To prevent this, you should always sanitize and validate user input before including it in a query.

// Sanitize and validate user input before using it in a MySQL query
$user_input = $_POST['user_input'];
$clean_input = mysqli_real_escape_string($connection, $user_input);

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

// Rest of the code to handle the query result