2015-11-05 48 views
0

我想一些值插入到數據庫後,將值插入數據庫,而是將它們插入到我要確保有對同一個用戶沒有重複的條目對同一offername之前和current date如何一些預檢查

我使用這個查詢,併爲userid工作的罰款和date

//checking for existing values 
$sql11=$Db1->query("SELECT COUNT(userid) AS total FROM TABLE where userid='$userid' and date BETWEEN (NOW() - INTERVAL 1 DAY) AND NOW()"); 
     $temp=$Db1->fetch_array($sql11); 
     $thismemberinfo['count']=$temp[total]; 
    // 
     if ($thismemberinfo['count'] >= 1) 
     { WARN HERE for duplicate entry} 
     else 
     { INSERT TO DB } 

我想包括offername進入檢查以及 類似的東西

SELECT COUNT(userid) AS total FROM TABLE where userid='$userid' and offername= '$offername' date BETWEEN (NOW() - INTERVAL 1 DAY) AND NOW() 

請推薦一個變通針對此問題

回答

0

注:

你不應該使用表名作爲TABLE和列名date因爲這兩個詞都保留在查詢中輸入。使用另一個名字。

爲了使您的查詢更加簡單,只需在添加之前同時選中列(user_id & date)即可。

看我的代碼,我會怎麼做。

//checking for existing values 
$sql11=$Db1->query("SELECT COUNT(userid) AS total FROM TABLE where userid='$userid' AND offername= '$offername' AND date BETWEEN (NOW() - INTERVAL 1 DAY) AND NOW()"); 
$temp = $Db1->fetch_array($sql11); 
$thismemberinfo['count']=$temp['total']; 
if ($thismemberinfo['count'] >= 1) { 
    // Just do nothing 
} else { 
    //INSERT TO DB 
    //Current date 
    $current_date = date('Y-m-d'); 

    $Db1->query("INSERT INTO TABLE (userid,offername,date) VALUES ('$user_id','$offername','$current_date')"); 
    //Table should be replace with suitable name since TABLE is a reserved word in query 
} 
+1

'$ temp [total]'不應該像這樣,直到或者除非你聲明'total'爲'CONSTANT',並且我不認爲它是'const'在這裏,所以它應該是'$ temp ['total']'。 – Mubin

+0

sql11 = $ Db1-> query(「SELECT COUNT(userid)AS FROM TABLE where userid ='$ userid'and offername ='$ offername'date BETWEEN(NOW() - INTERVAL 1 DAY)AND NOW().. ...根據你的反應,這個查詢是否正確...請重新檢查一下......我只是將許多「AND」混淆到查詢中。 –

相關問題