How can the relationship between form field data and database records be effectively maintained when updating multiple records with PHP?

When updating multiple records with PHP, it is important to ensure that the relationship between form field data and database records is maintained. This can be achieved by using a loop to iterate through the form data and update the corresponding database records accordingly.

// Assuming $formData is an array containing form field data and $db is your database connection

foreach($formData as $data){
    $id = $data['id']; // Assuming 'id' is the primary key in your database table
    $field1 = $data['field1']; // Assuming 'field1' corresponds to a field in your database table
    $field2 = $data['field2']; // Assuming 'field2' corresponds to another field in your database table
    
    $sql = "UPDATE your_table SET field1 = '$field1', field2 = '$field2' WHERE id = $id";
    $result = mysqli_query($db, $sql);
    
    if(!$result){
        echo "Error updating record with ID: " . $id;
    }
}