简体   繁体   中英

Selecting rows where column value is the same

I have a table, called 'files', where I store file_id's. Basically, users on my website can upload files to my website, and when a user uploads a file, all that file's info gets uploaded to my table.

Anyways, I'm working on a file manager where the users may see all the files that they have uploaded, edit the name of the file, delete the file, etc.

How I've done this is, when the file is uploaded, the user's unique id is uploaded along with the row with the unique file id.

So all I have to do for my file manager is match up the user's id with however many rows have said user's id in them, then display each file on the file manager page.

What is the easiest way to do this?

This is as far as I've gotten. Not great, I know.

$checkfiles = mysql_query("SELECT * FROM repository WHERE userid = '$useridtemp'") or die(mysql_error()); 
$fileinfo = mysql_fetch_array($checkfiles);

You're basically there:

$user_id_safe = AddSlashes($useridtemp);
$result = mysql_query("SELECT * FROM repository WHERE userid = '$user_id_safe'");
while ($row = mysql_fetch_array($result)){
    # this loop will run once for each found row, in $row
    print_r($row);
}

Just added an escape for the user id to avoid sql injection attacks, and loop over the returned rows.

This will print the files in a table, with delete and edit buttons. Obviously, this method is likely the least secure way to delete files... but I figured this is just educational, yes? Anyway, it's something.

echo '<table>';
foreach($fileinfo as $file) {
    echo '<tr>';
    // Print File info
    echo '<td>'.$file['filename'].'</td>';
    // Print Buttons
    echo '<td><a href="edit.php?id='.$file['id'].'"><img/></a></td>';
    echo '<td><a href="delete.php?id='.$file['id'].'"><img/></a></td>';
    echo '</tr>';
}
echo '</table>';

I've left the images blank. You'll have to find some icons yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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