douhuang5331 2019-05-19 16:31
浏览 265
已采纳

如何在PHP中创建mySQL数据库及其相应文件(超链接)的文件名HTML表

I have a form that submits a file to the server in the folder uploads and also saves the file name(as stored in the server) as well as other file details in my database. Now what I'm trying to do, is create a HTML table that displays file details as well as the corresponding file(In hyperlink of course).

Is there any way to this? An idea I had is to link the file name to the corresponding file in my uploads/ directory but I have no idea on how to achieve that.

Here is my table uploaded_content where the form data is stored in my database

================================================
id |Description | filename                     |
------------------------------------------------
1  |Information | 3223-2323-4334-32-slajjjq.txt|
2  |Users       | 3223-2323-4344-33-slik.txt   |
================================================

And here is an example of how my uploads/ directory looks like (Notice that their names are saved in my database)

3223-2323-4334-32-slajjjq.txt
3223-2323-4344-33-slik.txt

So now I want to create a HTML table with the following columns

id |Description | filename  |File   
  • 写回答

2条回答 默认 最新

  • dsh7551 2019-05-19 17:48
    关注

    It is not the best way to do it, but i just wanna show you how to do that.

    Step 1 - Create mysql DB.

    CREATE TABLE `uploaded_content` (
      `id` int(11) NOT NULL,
      `description` text NOT NULL,
      `filename` text NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    Step 2 - Insert data to this DB.

    INSERT INTO `uploaded_content` (`id`, `description`, `filename`) VALUES
    (1, 'Information', '3223-2323-4334-32-slajjjq.txt'),
    (2, 'Users', '3223-2323-4344-33-slik.txt');
    

    Step 3 - Make config.php:

    <?php
        $conn = new PDO('mysql:host=localhost;dbname=stack', 'root', '');
        $q = $conn->query("SELECT * FROM uploaded_content WHERE id");
        $f = $q->fetchAll();
    ?>
    

    Step 4 - Create index.php with this code:

    <?php
    include('config.php');
    ?>
    
    <table>
        <tr>
          <th>ID</th>
          <th>Description</th> 
          <th>File</th>
          <th>Download link</th>
        </tr>
    <?php 
    foreach ($f as $g) :?>
      <tr>
        <td> <?php echo $g['id']; ?></td>
        <td> <?php echo $g['description']; ?></td>
        <td> <?php echo $g['filename']; ?></td>
        <td><a href="https://example.com/uploads/<?php echo $g['filename']; ?>">Download</a></td>
      </tr>
    <?php endforeach;?>
    </table>
    

    When you finish that, you get html page like this:

    <table>
        <tr>
          <th>Id</th>
          <th>Description</th> 
          <th>File</th>
          <th>Download link</th>
        </tr>
        <tr>
          <td>1</td>
          <td>Information</td>
          <td>3223-2323-4334-32-slajjjq.txt</td>
          <td><a href="https://example.com/uploads/3223-2323-4334-32-slajjjq.txt">Download</a></td>
        </tr>
        <tr>
          <td>2</td>
          <td>Users</td>
          <td>3223-2323-4344-33-slik.txt</td>
          <td><a href="https://example.com/uploads/3223-2323-4344-33-slik.txt">Download</a></td>
        </tr>
    </table>

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?