2011-10-07 61 views
-1

由於我是新來的PHP,我發現這個極其複雜的解決。我有這個查詢顯示用戶不應該這樣做的結果。多表查詢加入

的問題是銷售代理能夠看到他沒有被授權的用戶看到的投訴。這涉及用於客戶的賬戶表。

$priv = "dire problem" , 
$naone = "not serious" , 
$priv2 = "mild prblem" 
are sorting conditions. 
$aid is the agent viewing this page. 

Complaints is for complaints by the customers. 
Accounts table holds all the customer information. 
Agents table is for all the sales/customer reps. 

代碼:

$sql = "SELECT complaints.complaint_id, accounts.full_name, 
agents.agent_name, complaints.person_id, complaints.why_complaint, 
complaints.just_date, complaints.type, complaints.date_time_added FROM 
complaints LEFT JOIN accounts ON complaints.person_id = accounts.person_id 
LEFT JOIN agents on complaints.agent_whois = agents.agent_id WHERE 
(complaint_type = '$priv' OR complaint_type = '$naone' OR complaint_type = '$priv2') and 
(complaints.added_by <> '$aid')"; 
$result=mysql_query($sql); 

$query = mysql_query($sql) or die ("Error: ".mysql_error()); 

if ($result == "") 
{ 
echo ""; 
} 
echo ""; 


$rows = mysql_num_rows($result); 

if($rows == 0) 
{ 
print(""); 

} 
elseif($rows > 0) 
{ 
while($row = mysql_fetch_array($query)) 
{ 

    $complaintid = $row['complaint_id']; 
    $agentwho = $row['person_id']; 
    $agentname = $row['agent_name']; 
$reason = $row['why_complaint']; 
$datetimeadded = $row['just_date']; 
    $docname = $row['full_name']; 
    $type = $row['type']; 


    print(""); 
    } 

    } 
+0

你想解決什麼問題? – str

+0

我與這個問題ediiting它 – AAA

+0

困惑,你試圖限制視圖只顯示那些已被分配給該代理,或者是你想將其限制在經許可的一級代理商? – espradley

回答

1

這是不是一個真正的答案,但我無論如何張貼它,因爲它是我能做到最好。

OK,首先,你的縮進所有的地方。 ,爲了任何需要閱讀您的代碼的人,請使用一致的indentation style。使用—的哪種款式只需選擇一種並始終如一地應用它並不重要。它使你的代碼更容易閱讀。

這就是說,讓我們來看看您的查詢。這是在其原來的形式,只是reindented爲更好的可讀性:

SELECT 
    complaints.complaint_id, 
    accounts.full_name, 
    agents.agent_name, 
    complaints.person_id, 
    complaints.why_complaint, 
    complaints.just_date, 
    complaints.type, 
    complaints.date_time_added 
FROM 
    complaints 
    LEFT JOIN accounts ON complaints.person_id = accounts.person_id 
    LEFT JOIN agents ON complaints.agent_whois = agents.agent_id 
WHERE 
    (complaint_type = '$priv' 
    OR complaint_type = '$naone' 
    OR complaint_type = '$priv2') 
    AND (complaints.added_by <> '$aid') 

事實上,我們可以更多的緊湊改寫WHERE條款是這樣的:

WHERE 
    complaint_type IN ('$priv', '$naone', '$priv2') 
    AND complaints.added_by <> '$aid' 

但是,所有這說的是, complain_type必須是三個值之一,並且該投訴不能由代理人'$aid'添加。你說

「問題是銷售代理能夠看到他沒有被授權查看的用戶的投訴。」

但在查詢中絕對沒有關於任何類型的授權!由於我甚至無法從查詢中猜出您的表可能包含的授權數據類型,或者您想要對其執行的操作,我可以給您的唯一建議是找出一些規則來告訴應該顯示的記錄從那些不應該和將它們添加到查詢