What best practices should be followed when structuring PHP code for displaying and updating multiple database records using forms?

When structuring PHP code for displaying and updating multiple database records using forms, it is important to use a loop to iterate through each record and generate corresponding form elements. Each form element should be named in a way that allows easy identification of the record it belongs to when processing the form submission. Additionally, the form submission should be handled by looping through the submitted data and updating the corresponding database records.

<?php
// Retrieve records from the database
$records = // Query to retrieve records from the database

// Display form for each record
foreach ($records as $record) {
    echo "<form method='post'>";
    echo "<input type='text' name='record[".$record['id']."][field1]' value='".$record['field1']."'>";
    echo "<input type='text' name='record[".$record['id']."][field2]' value='".$record['field2']."'>";
    // Add more form elements as needed
    echo "<input type='submit' name='update' value='Update'>";
    echo "</form>";
}

// Handle form submission
if (isset($_POST['update'])) {
    foreach ($_POST['record'] as $id => $data) {
        // Update database record with id $id using $data
    }
}
?>