How can the issue of data duplication in a PHP form be resolved when editing existing records?

Issue: The problem of data duplication in a PHP form when editing existing records can be resolved by pre-populating the form fields with the existing data before allowing the user to make changes. Code snippet:

<?php
// Assume $existingData is an array containing the existing data for the record being edited

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    // Update record in database
    // Redirect to success page
} else {
    // Pre-populate form fields with existing data
    foreach ($existingData as $key => $value) {
        echo '<input type="text" name="' . $key . '" value="' . $value . '">';
    }
    echo '<button type="submit">Submit</button>';
}
?>