What is the best way to display text from a database in an input field or textarea in PHP?

When displaying text from a database in an input field or textarea in PHP, it is important to properly sanitize the data to prevent any potential security vulnerabilities such as SQL injection attacks. One way to do this is by using the htmlspecialchars() function to escape special characters before outputting the text. This will ensure that the text is displayed correctly and safely in the input field or textarea.

<?php
// Retrieve text from database
$text = $row['text'];

// Sanitize the text before displaying in input field or textarea
$sanitized_text = htmlspecialchars($text);

// Display text in input field
echo '<input type="text" value="' . $sanitized_text . '">';

// Display text in textarea
echo '<textarea>' . $sanitized_text . '</textarea>';
?>