What are the potential risks of not properly escaping or masking values before inserting them into a database using PHP?

If values are not properly escaped or masked before inserting them into a database using PHP, it can leave the application vulnerable to SQL injection attacks. This means that malicious users could potentially manipulate the database by injecting harmful SQL code into the input fields. To prevent this, it is important to always sanitize and escape user input before using it in SQL queries.

// Example of properly escaping user input before inserting into a database
$unsafe_input = $_POST['user_input'];
$safe_input = mysqli_real_escape_string($connection, $unsafe_input);

$query = "INSERT INTO table_name (column_name) VALUES ('$safe_input')";
mysqli_query($connection, $query);