What are the potential pitfalls of directly inserting user input into a MySQL database in PHP?

Directly inserting user input into a MySQL database in PHP can lead to SQL injection attacks, where malicious SQL code is inserted into the input fields, potentially allowing attackers to manipulate the database or steal sensitive information. To prevent this, it is important to sanitize and validate user input before inserting it into the database.

// Sanitize and validate user input before inserting into the database
$userInput = $_POST['user_input'];
$cleanInput = mysqli_real_escape_string($connection, $userInput);
// Perform additional validation if necessary
$query = "INSERT INTO table_name (column_name) VALUES ('$cleanInput')";
mysqli_query($connection, $query);