How can the issue of new data being appended to old data in .db files be resolved when adding new records in PHP?

When adding new records to a .db file in PHP, the issue of new data being appended to old data can be resolved by truncating the file before writing the new data. This ensures that only the new records are stored in the file without any old data lingering.

$file = 'data.db';
$data = "New record data";

// Open the file for writing and truncate it
$handle = fopen($file, 'w');
fwrite($handle, $data);
fclose($handle);