Skip to content
PHP Logo

How can HTML code generated by WYSIWYG editors be securely stored in a database?

When storing HTML code generated by WYSIWYG editors in a database, it's important to properly sanitize the input to prevent SQL injection attacks and ensure that the stored HTML code does not contain any malicious scripts. One way to securely store HTML code is to use PHP's htmlspecialchars function to encode special characters before inserting the data into the database.

php
// Assuming $htmlCode contains the HTML code generated by the WYSIWYG editor

// Sanitize the HTML code before storing it in the database
$sanitizedHtmlCode = htmlspecialchars($htmlCode);

// Insert the sanitized HTML code into the database
$query = "INSERT INTO table_name (html_code_column) VALUES ('$sanitizedHtmlCode')";
mysqli_query($connection, $query);