dslpofp041310584 2017-03-03 21:07
浏览 29
已采纳

使用两个SQL表中的标题创建一个HTML表

I am trying to create a simple file upload system using PHP and MySQL. I have created a database with two tables in, structure below:

File Table

+---+------------------+--------------+
| # |       Name       |     Type     |
+---+------------------+--------------+
| 1 | file_id          | int          |
| 2 | file_name        | varchar      |
| 3 | file_size        | int          |
| 4 | file_type        | varchar      |
| 5 | folder           | int          |Foreign Key to folders.folder_id
| 6 | user_Id          | int          |
| 7 | upload_time      | timestamp    |
| 8 | modify_time      | timestamp    |
+---+------------------+--------------+

Folders Table

+---+------------------+--------------+
| # |       Name       |     Type     |
+---+------------------+--------------+
| 1 | folder_id        | int          |
| 2 | name             | varchar      |
| 3 | subfolder        | int          |
+---+------------------+--------------+

Files Query

    SELECT * 
FROM   files 
       JOIN folders 
         ON folders.folder_id = files.folder 
ORDER  BY folders.subfolder ASC, 
          folders.folder_id ASC 

Folders Query

SELECT * FROM folders

The tables both echoed into a HTML table. To do this, I have two query runs happening separately, one which echoes the folders and displays them on the table, and the other echoes the files and lists them below the list of folders.

Rather than listing all the folders and then all the files I want the files to be listed below their relevant folders.

This is how my table is currently displayed:

Table Heading
-------------
folder
folder2
folder3
file1
file2
file3
file4

This is how I want it to be:

Table Heading
-------------
folder
file1
file2
folder2
file3
folder3
file4
  • 写回答

2条回答 默认 最新

  • duandu8892 2017-03-03 21:27
    关注

    Prepared statements are designed to be executed multiple times in a loop, this is a good example of where to use one:

    <?php
    // prepare the statement for use later
    $folderId = 0;
    $stmt = $connectionUploads->prepare("SELECT * FROM files WHERE folder = ?");
    $stmt->bind_param("i", $folderId);
    
    $queryGetFolders = "SELECT * FROM folders";
    $resultGetFolders = mysqli_query($connectionUploads, $queryGetFolders);
    
    if ($resultGetFolders->num_rows != 0) {
        // loop through the folder list
        while ($rowFolders = $resultGetFolders->fetch_assoc()) {
            $folderName = ucwords($rowFolders['name']);
            echo "<h1>$folderName</h1>";
    
            // put the new value in $folderId
            $folderId = $rowFolders['folder_id'];
            // execute the query
            $stmt->execute();
            $resultGetFiles = $stmt->get_result();
            if ($resultGetFiles->num_rows == 0) {
                echo "<p>No files</p>";
            else {
                // loop through the results as normal
                while ($rowFiles = $resultGetFiles->fetch_assoc()) {
                    echo "<p>$rowFiles[file_name]</p>";
                }
            }
        }
    }
    

    Note there are no inherent security concerns with using "plain" database queries. The problems arise when you're adding user-supplied inputs to the query string. Then prepared statements are a must. In addition to reducing overhead when querying in a loop (as above) they also sanitize user input automatically.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)