What are common pitfalls when trying to save and reuse user input from a text field in PHP?

Common pitfalls when trying to save and reuse user input from a text field in PHP include not properly sanitizing the input data, leaving the application vulnerable to SQL injection attacks, and not escaping the output data, which can lead to cross-site scripting (XSS) vulnerabilities. To solve these issues, always sanitize and validate user input before saving it to the database, and escape the output data before displaying it to the user.

// Sanitize and validate user input
$user_input = $_POST['user_input'];
$clean_input = filter_var($user_input, FILTER_SANITIZE_STRING);

// Save the sanitized input to the database
// $sql = "INSERT INTO table_name (column_name) VALUES ('$clean_input')";
// Execute the SQL query

// Escape the output data before displaying it
echo htmlspecialchars($clean_input);