What are the potential risks of allowing users to input HTML code into a database?

Allowing users to input HTML code into a database can pose security risks such as cross-site scripting (XSS) attacks, where malicious scripts can be executed on other users' browsers. To prevent this, it is important to sanitize and validate user input before storing it in the database. This can be done by using functions like htmlspecialchars() to escape special characters and strip_tags() to remove any potentially harmful HTML tags.

// Sanitize and validate user input before storing in the database
$userInput = "<script>alert('XSS attack!');</script>";
$sanitizedInput = htmlspecialchars($userInput);
$validatedInput = strip_tags($sanitizedInput);

// Store the sanitized and validated input in the database
// Example query:
// $query = "INSERT INTO table_name (column_name) VALUES ('$validatedInput')";