【问题标题】:Selecting rows where column value is the same选择列值相同的行
【发布时间】:2012-04-03 22:46:25
【问题描述】:

我有一个名为“files”的表,用于存储 file_id。基本上,我网站上的用户可以将文件上传到我的网站,当用户上传文件时,该文件的所有信息都会上传到我的表中。

无论如何,我正在开发一个文件管理器,用户可以在其中查看他们上传的所有文件、编辑文件名、删除文件等。

我的做法是,当文件上传时,用户的唯一 id 与具有唯一文件 id 的行一起上传。

所以我为我的文件管理器所要做的就是匹配用户的 id 与其中许多行都说用户的 id,然后在文件管理器页面上显示每个文件。

最简单的方法是什么?

这是我所知道的。不太好,我知道。

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

【问题讨论】:

  • 好的,那么问题出在哪里?您走在正确的轨道上,这将为您提供一组在 userid 列中具有该用户 id 的行。现在您需要遍历它们并拉出 file_id 列。

标签: php mysql rows file-management


【解决方案1】:

你基本上就在那里:

$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);
}

只是为用户 ID 添加了转义以避免 sql 注入攻击,并循环返回的行。

【讨论】:

    【解决方案2】:

    这将在表格中打印文件,带有删除编辑按钮。显然,这种方法可能是删除文件最不安全的方法......但我认为这只是教育,是吗?无论如何,它是一些东西。

    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>';
    

    我将图像留空。 You'll have to find some icons yourself.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 2014-12-08
      相关资源
      最近更新 更多