What are the potential issues with overwriting existing data in a CSV file when adding new records in PHP?

Potential issues with overwriting existing data in a CSV file when adding new records in PHP include losing previous data, creating duplicate entries, and potentially corrupting the file. To solve this issue, you can read the existing data from the CSV file, append the new records to it, and then rewrite the entire contents back to the file.

// Read existing data from the CSV file
$existingData = file_get_contents('data.csv');

// Append new records to the existing data
$newRecords = "New Record 1, New Record 2\n";
$updatedData = $existingData . $newRecords;

// Write the updated data back to the CSV file
file_put_contents('data.csv', $updatedData);