2013-03-11 73 views
0

我怎樣才能讓PHP刪除限制用戶每月刪除用戶搜索限制:: MYSQL

我有這樣的代碼來增加一個用戶的搜索限制,如果他們達到其極限

$search = $db->prepare("SELECT `searched` FROM `search` WHERE `user_id` =:user_id"); 
$search->bindValue(':user_id', $vbulletin->userinfo['userid'], PDO::PARAM_INT); 
$search->execute(); 
$result = $search->fetch(PDO::FETCH_ASSOC); 
if ($result['searched'] >= 100){ 
    exit('You have reached your search limit for this month.<br>'); 
} 
$addsearch = $db->prepare("UPDATE `evomap`.`search` SET `searched` = :searched + 1 WHERE `search`.`id` =:user_id;"); 
$addsearch->bindValue(':user_id', $vbulletin->userinfo['userid'], PDO::PARAM_INT); 
$addsearch->bindValue(':searched', $result['searched'], PDO::PARAM_STR); 
$addsearch->execute(); 
/* page i want to execute if user has no reached limit */ 
+0

您是否需要存儲用戶搜索了多少?你可以每月一次截斷表格。 – Duniyadnd 2013-03-11 00:20:25

回答

1

您可能最好有一個包含月份時間戳的列,設置爲日期,當用戶達到100次搜索時,然後檢查當前月份是否爲!= timestamp。但是你也可以嘗試MySQL事件。例如:

DELIMITER $$ 
    CREATE EVENT search_removal 
    ON SCHEDULE EVERY MONTH 
    DO 
     BEGIN 
     UPDATE search SET searched = 0; 
     END; 
    $$; 
+0

所以我可以做;如果($ result ['month']!= time(n)){ $ db-> query(「UPDATE'evomap'''search' SET'month' = month(now())WHERE'search' .'user_id' =:user_id「); } 它看起來應該是足夠的 – DrWhat 2013-03-12 13:44:37