0

Recently I've been working on a PHP/MySQL script which reads information from a database with user info and file info stored in two separate tables.

Below are the schema's for the tables.

Table1

UID | Username | PermissionLevel
1   | First    | 1
2   | Next     | 3
3   | More     | 2

Table2

FID | Filename | FileLevel | UploadUsername
1   | file.txt | 2         | First
2   | hand.mp4 | 1         | First
3   | 1245.dds | 1         | Next
4   | beta.sql | 3         | More

For the purpose of this message I have omitted the passwords column and the file title/description column, since they play no part in the result I am trying to achieve.

So far I have come up with this SQL code

SELECT DISTINCT table2.*, 
table1.*
FROM table2 JOIN table2 ON table2.FileLevel <= table1.PermissionLevel
WHERE table2.UploadUsername = table1.Username 
ORDER BY FID DESC LIMIT 7

This generates the appropriate listing I want, but does not filter the level of content shown.

Any user with a PermissionLevel of 1 should only see Files with a FileLevel of 1. Users with PermissionLevel of "2" can see files of FileLevel of both 2 AND 1, and so on.

But at the current stage it seems to just want to display ALL results regardless of File/Permission Level.

I've been stuck at this issue for a couple of days now and just can't seem to get my head around this. It's likely to be something simple I may have overlooked, but I hope that a fresh pair of eyes may help me.

4

1 回答 1

0

我不确定我是否完全理解这个问题,但这是我能做到的。您有一个返回所有上传文件的查询。您现在想要过滤该列表,因此它有时会显示较少的结果,具体取决于用户的权限级别。请注意,此用户是网站上的活跃用户,不一定是上传文件的同一用户,因此此条件不适table1用于您的查询表。

一个常见的解决方案是使用一个会话变量来存储您当前用户的 id 以及可能的其他信息。

$_SESSION['user'] = 'somebody';
$_SESSION['permissionLevel'] = 3;

如果您没有permissionLevel本地变量,则必须在table1表上加入两次,一次是查找上传者,一次是查找当前用户的权限级别。您的原始查询中也有错字。以下将为您提供两个用户

SELECT *
FROM table2
JOIN table1 uploader WHERE table2.UploadUsername=table1.Username
JOIN table1 currentuser WHERE table2.PersmissionLevel<=table1.PermissionsLevel AND users.Username='$_SESSION[user]'
ORDER BY FID DESC 
于 2013-04-19T22:07:00.490 回答