What are potential security risks when using user input directly in PHP code?

When using user input directly in PHP code, there is a risk of SQL injection, cross-site scripting (XSS), and other security vulnerabilities. To mitigate these risks, it is essential to sanitize and validate user input before using it in your code.

// Sanitize and validate user input before using it in your code
$userInput = $_POST['user_input'];

// Sanitize the user input to prevent SQL injection
$sanitizedInput = mysqli_real_escape_string($connection, $userInput);

// Validate the user input to prevent XSS
$validatedInput = htmlspecialchars($sanitizedInput);

// Now you can safely use $validatedInput in your PHP code