What are the common issues that may arise when saving user input in PHP and how can they be resolved?

Issue: One common issue when saving user input in PHP is the lack of proper validation and sanitization, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To resolve this, always validate and sanitize user input before saving it to the database.

// Validate and sanitize user input before saving
$userInput = $_POST['user_input'];

// Validate input
if(!empty($userInput)) {
    // Sanitize input
    $sanitizedInput = filter_var($userInput, FILTER_SANITIZE_STRING);

    // Save sanitized input to the database
    // Example: $db->query("INSERT INTO table_name (column_name) VALUES ('$sanitizedInput')");
} else {
    echo "Invalid input";
}