Unanswered: How to know when to use insert or update?
I have a DB table that holds some times and a user_id and date, every user can only have one record per day (date). On the webpage the user can enter times and these should be written into the DB table, but how do I know if the record for that day already exists or not, if it exists I must use update to alter the times for that day, if not I must use insert.
Do I have to do a select first?? ..or running an update first and see if any rows was affected, if it was all is fine... if not try insert!
What I did try first was to do a select to get all the current times and set my input boxes, so the user can alter them. And at the same time store the tables unique autoinc. id set this in the hidden fields of the records found, and use that to distinguish between when to use insert and when to use update. If the hidden field contained an id the records exist and I should use update, if not I used insert. But I did get errors this way, some users got duplicate records (same date for several records for one user) Why??? I thought about it, it might have been the user hitting F5 to reload the page or?? I don't know, but is there anyone here who has a good solution for this dilemma? A select before every insert to check if it exist must slow the process down quite a bit I guess. Currently I'm going for the update first and checking to see if any rows was affected... is there a better solution?