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 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?