2011-10-07 47 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'添加。你说

“问题是销售代理能够看到他没有被授权查看的用户的投诉。”

但在查询中绝对没有关于任何类型的授权!由于我甚至无法从查询中猜出您的表可能包含的授权数据类型,或者您想要对其执行的操作,我可以给您的唯一建议是找出一些规则来告诉应该显示的记录从那些不应该和将它们添加到查询