What are the best practices for error handling in PHP when updating database records based on user input?

When updating database records based on user input in PHP, it is important to handle errors gracefully to provide a better user experience and prevent potential security vulnerabilities. One best practice is to validate user input before updating the database to ensure data integrity. Additionally, using prepared statements with parameterized queries can help prevent SQL injection attacks.

// Validate user input
if(isset($_POST['user_input']) && !empty($_POST['user_input'])) {
    $user_input = $_POST['user_input'];
    
    // Update database record with prepared statement
    $stmt = $pdo->prepare("UPDATE table SET column = :user_input WHERE id = :id");
    $stmt->bindParam(':user_input', $user_input);
    $stmt->bindParam(':id', $id);
    
    if($stmt->execute()) {
        echo "Record updated successfully.";
    } else {
        echo "Error updating record.";
    }
} else {
    echo "Invalid user input.";
}