Add to the database after updating all relevant variables or text fields. So do '**** ADD to database section after '***** End of Output *****

And I suggest you design your tables with primary keys.