多表查询连接

发布于 2024-12-09 03:37:27 字数 1471 浏览 3 评论 0原文

由于我是 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("");
   }

    }

Since I am new to PHP, i am finding this immensely complicated to solve. I have this query that is showing results that a user is not supposed to do.

The problem is the sales agent is able to see complaints from users he is not authorized to see. This pertains to the accounts table, which is for the customers.

$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. 

Code:

$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("");
   }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2024-12-16 03:37:27

这并不是一个真正的答案,但无论如何我都会发布它,因为这是我能做的最好的事情。

好吧,首先,你的缩进到处都是。 ,为了任何需要阅读您的代码的人,请使用一致的缩进风格。使用哪种风格并不重要——只需选择一种并一致应用即可。它使您的代码更易于阅读。

也就是说,让我们看一下您的查询。这是它的原始形式,只是为了更好的可读性而重新缩进:

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' 添加。你这么说

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

但查询中绝对没有任何关于任何类型授权的内容!由于我什至无法从查询中猜测您的表可能包含哪种授权数据,或者您想用它做什么,所以我能给您的唯一建议是找出一些规则来告诉应该显示的记录从那些不应该的,将它们添加到查询中

This is not really an answer, but I'm posting it anyway since it's the best I can do.

OK, first of all, your indentation is all over the place. Please, for the sake of anyone who needs to read your code, use a consistent indentation style. It doesn't really matter which style you use — just pick one and apply it consistently. It makes you code much easier to read.

That said, let's take a look at your query. Here it is in its original form, just reindented for better readability:

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')

In fact, we can rewrite the WHERE clause a bit more compactly like this:

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

But all this says is that complain_type must be one of the three values, and that the complaint must not have been added by the agent '$aid'. You say that

"The problem is the sales agent is able to see complaints from users he is not authorized to see."

but there's absolutely nothing in the query about any kind of authorization at all! Since I can't even guess from the query what kind of authorization data your tables might contain, or what you want to do with it, the only advice I can give you is to figure out some rules to tell the records that should be shown from those that shouldn't be and add them to the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文