Selecting rows where column value is the same

Go To StackoverFlow.com

0

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);
2012-04-03 22:46
by Hugo Cornellier
OK, so where's the problem? You're on the right track, that will give you an array of rows that have that user id in the userid column. Now you need to loop over them and pull the file_id column - Paystey 2012-04-03 22:50


1

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.

2012-04-03 22:49
by Cal
Thankyou! This code worked perfectly - Hugo Cornellier 2012-04-03 23:22


0

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.

2012-04-03 22:54
by savinger
Thanks! This worked perfectly - Hugo Cornellier 2012-04-03 23:22