简体   繁体   English

PHP的MySQL,如果ID在领域

[英]php mysql if id in field

My database 我的资料库

comments 评论

ID   mID   aID   attachID
1     1     6       1       
2     2     6       1
3     3     6       1
4     8     6       2
5     4     6       2

So i'd like to allow access to user with mID to access attachID. 所以我想允许访问具有mID的用户以访问attachID。

If they are viewing attachID 1, so users with mID of 1 or 2 or 3 can view. 如果他们正在查看attachID 1,则mID为1或2或3的用户可以查看。 rest mID cannot. 休息mID不能。

EDIT : for attachID 1, users with mID 1 can see which other mID user can view too. 编辑:对于attachID 1,具有mID 1的用户也可以看到其他mID用户也可以查看。

What would be the best approach to accomplish this? 什么是实现这一目标的最佳方法?

function authorize($m_id, $a_id) {
   $query = <<<SQL
      SELECT
         TRUE
      FROM
         db.Table
      WHERE
         m_id = $m_id
         AND a_id = $_aid
SQL;
   return mysql_num_rows(mysql_query($query));
}

This function will return TRUE if m_id is authorized for a_id, false otherwise. 如果m_id被授权使用a_id,则此函数将返回TRUE,否则返回false。 Then on your php page you have something like: 然后在您的php页面上,您会看到类似以下内容的内容:

if (!authorize($m_id, $a_id)) {
   header('Location: /not_authorized.php');
}
// Show normal apge for authorized user

If returns 0, user isn't authorized to view: 如果返回0,则用户无权查看:

select count(1) from comments where mID = '$mID' and attachID = '$attachID'

Get all mIDs for a given attachID which can then be displayed to the user viewing the given attachID: 获取给定attachID的所有mID,然后可以将其显示给查看给定attachID的用户:

select mID from comments where attachID = '$attachID'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM