How can PHP be used to allow users to format text input without compromising database security?

When allowing users to format text input in PHP without compromising database security, it is important to sanitize the input to prevent SQL injection attacks. This can be done by using prepared statements and parameterized queries to securely interact with the database. Additionally, implementing input validation to only allow certain HTML tags or formatting options can help prevent cross-site scripting attacks.

// Example of using prepared statements to insert formatted text input into a database

// Assuming $conn is the database connection object

// Sanitize and validate user input
$formattedText = $_POST['formatted_text']; // Assuming this is the user input
// Validate and sanitize the input further as needed

// Prepare the SQL statement
$stmt = $conn->prepare("INSERT INTO table_name (formatted_text) VALUES (?)");
$stmt->bind_param("s", $formattedText);

// Execute the statement
$stmt->execute();

// Close the statement and connection
$stmt->close();
$conn->close();