使用户只能看到他们自己上传的文件

Good afternoon all,

I'm in the finishing stages of making a website where users can register and login. After that they can upload documents such as .pdf and .docx and search for those documents. The users data gets stored in my users table which contains following:

idUsers int(11) PRIMARY KEY NOT NULL,  
uidUsers TINYTEXT NOT NULL,  
emailUsers TINYTEXT NOT NULL,  
pwdUsers LONGTEXT NOT NULL,

Then I have a table called "files" where the files that are being uploaded by the user i stored. The table contains following:

id int(11) PRIMARY KEY NOT NULL, 
usersId int(11) NOT NULL, - (Foreign key for idUsers in  users table)
name varchar(255) NOT NULL,
title varchar(255) NOT NULL,
forfatter varchar(255) NOT NULL, (forfatter = author) 
size int(11), 
download int(11) (for later use)

So when a specific user are logged in and uploads document, the users "id" (primary key in users table) gets passed to "usersId" in my files table.

All of this is working perfectly fine so far.

Then the user are able to use a search function, where the user can search for name, title and author of the document, which also works fine.

I only need to make sure the user only can see the documents, that are being uploaded by themselves. But I can't figure it out. I've tried many different solutions so far.

I've tried a suggestion i saw, where i tried to make my mysqli_query look like this:

"
SELECT * 
  FROM files 
 WHERE id 
   AND usersId name LIKE '%$searchingq%' 
    OR title LIKE '%$searchingq%' 
    OR forfatter LIKE '%$searchingq%' 
   AND usersId='.$usersId.'"

That wasn't working out well. It felt like the sessions wasn't finding the id of the current $_SESSION.

My search .php looks like this:

    $usersId = $_SESSION['userId'];

    $output = '';

    if (isset($_GET['search']) && $_GET['search'] !== ' ') {
        $searchingq = $_GET['search'];

        $q = mysqli_query($conn, "SELECT * FROM files WHERE `usersId` = {$usersId} AND (`name` LIKE '%{$searchingq}%' OR `title` LIKE '%{$searchingq}%' OR `forfatter` LIKE '%{$searchingq}% )"); 

        $c = mysqli_num_rows($q);

        if($c == 0) {
            $output = '<p>No search results for: "' .$searchingq. '"</p>';
        } else {


            while($row = mysqli_fetch_array($q)) {
                $name = $row['name'];
                $title = $row['title'];
                $forfatter = $row['forfatter'];
                $download = $row['downloads'];

                $output .= 

Help is very appreciated!

Maybe you could try adding AND usersId=".$userId to the end of your query.

When doing code like this, I like to first run the query itself just to get it right. This type of filtering should be easy because you already have each file associated to a user.

The base query would be like this:

Assuming you are looking for files for user 99.

This is the basic query and is intended only to show how to get the files for a user. This does not go into the code.

SELECT * FROM files
WHERE `usersId` = 99

This would give you only files that belong to that user. Again, this is only to explain the query, not to go in the code.

Now let's add to the above query to include the search criteria. Assume we are looking for 'foo'

SELECT * FROM files
WHERE `usersId` = 99
AND (`name` LIKE '%foo%'
    OR `title` LIKE '%foo%'
    OR `forfatter` LIKE '%foo%' )

We now have a query we can use in code. Try this out by running it directly against your database.

This query says to find all the files for that user when any of the columns match the search criteria. You can, therefore, write this in PHP something like this:

(Some code has been removed for the sake of brevity)

$usersId = $_SESSION['userId']; // the user id of the logged in user.
// code removed
$searchingq = $_GET['search']; // the search string
$q = mysqli_query($conn, "
SELECT * FROM files
WHERE `usersId` = {$usersId}
AND (`name` LIKE '%{$searchingq}%'
    OR `title` LIKE '%{$searchingq}%'
    OR `forfatter` LIKE '%{$searchingq}% )";

while($row = mysqli_fetch_array($q)) {

    // Do output here

}

The following is how to do it with prepared statements. You can learn more about Prepared Statements here: https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

$stmt = $mysqli_prepare($conn, "
    SELECT * FROM files
      WHERE `usersId` = ?
      AND (`name` LIKE '%?%'
        OR `title` LIKE '%?%'
        OR `forfatter` LIKE '%?% )");
 // Replace the ?'s with the actual values.
 mysqli_stmt_bind_param($stmt, "isss", $usersId, $searchingq, $searchingq, $searchingq);

 mysqli_stmt_execute($stmt);
 $result = mysqli_stmt_get_result($stmt);
 while ($row = mysqli_fetch_assoc($result)) {
     // output goes here
 }