Answer the question
In order to leave comments, you need to log in
What is the best way to do validation when adding data to mysql?
There is data that needs to be reviewed after each save, for example, this is a user access table. After the moment you click "Save", you need to re-check if each of the records is in the table.
Option 1: select all records belonging to object A, add necessary / delete unnecessary ones.
Option 2: delete all records belonging to object A, add all new ones.
Now I'm planning "option 2". What's better?
Answer the question
In order to leave comments, you need to log in
Add field `updated` TINYINT(1), add UNIQUE KEY (`user_id`, `obj_id`).
Before changing, reset the flag:
When changing, we mark the added / duplicated entries:
INSERT INTO `table` (`user_id`, `obj_id`, `updated`) VALUES ($uid, $oid, 1)
ON DUPLICATE KEY UPDATE `updated` = 1
DELETE FROM `table` WHERE `user_id` = $uid AND `updated` = 0
I have a similar situation with a linking category table.
Personally, my option is to delete and re-record, the first option will take more resources, which is not very good
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question