M
M
Mikhail Shatilov2014-03-15 21:53:49
PHP
Mikhail Shatilov, 2014-03-15 21:53:49

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

3 answer(s)
R
Rsa97, 2014-03-16
@iproger

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

After the change, delete the unmarked entries:
DELETE FROM `table` WHERE `user_id` = $uid AND `updated` = 0

A
Alexander Khirenko, 2014-03-15
@Satanpit

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

P
Push Pull, 2014-03-15
@deadbyelpy

If such questions arise, then the structure for storing access rights is incorrectly selected.
If there is nothing to fix, yes, option 2 is less expensive. Less checks.
But, the recommendation is still to change the storage structure

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question