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条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答