开发者

Put a flag when a user visit a topic : trouble

开发者 https://www.devze.com 2023-03-02 09:52 出处:网络
Suppose I have a sort of forum with topics that users/admins can visit/manage. When a user visit it, first I\'ll check if the topic exist; after I put a flag for say to the system if the user has be

Suppose I have a sort of forum with topics that users/admins can visit/manage.

When a user visit it, first I'll check if the topic exist; after I put a flag for say to the system if the user has been visited the topic.

This would be the basic code :

1        if((isset($_GET['trackid'])) && (ctype_digit($_GET['trackid']))) {
2            $query=mysql_query("SELECT date, user, zone, artist, event, data, res, complete, notes FROM topic WHERE trackid='".$_GET['trackid']."'",$mydb);    
3            if(mysql_num_rows($query)!=0) {
4                echo "Ok, The topic exist! Now I can say to the system开发者_如何学编程 that I have visited the topic"
5                
6                if (isset($_SESSION['nickname'])) $insert=mysql_query("INSERT IGNORE INTO recent_adds (user, trackid) VALUES ('".$_SESSION['nickname']."','".$_GET['trackid']."')",$mydb);                 
7            }
8        }

Now, suppose that User A arrive to the line 4, and meanwhile an admin (User B) remove this topic (trought a special function for the admins, but this doesn't mean so much) : it will insert a inconsistent data to the database. (in fact that topic won't be exist anymore).

How the major forum/system that do this kind of operation get rid about this concurrency problem?


You can use FOREIGN KEYS to disallow inserting anything which have been deleted. Then you also get the benefit of doing a cascading delete.

Another solution is to run a batch job which cleans out inconsistent data. Along the lines of this solution, do not really delete the topic. Just mark it as deleted with a status flag and delete it later.

0

精彩评论

暂无评论...
验证码 换一张
取 消