What are the potential security risks associated with not properly escaping user input in PHP code?

Not properly escaping user input in PHP code can lead to security risks such as SQL injection, cross-site scripting (XSS), and code injection attacks. To mitigate these risks, user input should be properly sanitized and escaped before being used in SQL queries, output to the browser, or executed as code.

// Example of properly escaping user input in PHP code
$userInput = $_POST['user_input']; // Assuming user input is coming from a form submission

// Sanitize and escape user input before using it in a SQL query
$escapedUserInput = mysqli_real_escape_string($connection, $userInput);
$query = "SELECT * FROM users WHERE username = '$escapedUserInput'";
$result = mysqli_query($connection, $query);

// Output escaped user input to the browser to prevent XSS attacks
echo htmlspecialchars($userInput);