doubeng1278 2014-02-11 14:42
浏览 12
已采纳

使用mysql中的3个表使用php创建下载脚本

I need help from you guys here.

The problem is I have to display download link from a table that connected from other tables, there are three (3) tables.

**First Table:**

file_id | file_title | file_name | file_dir
-------------------------------------------
        |            |           |


**Second Table:**

file_id | books_id
-------------------


**Third Table:**

books_id | books_title | books_author | books_publisher
-----------------------------------------------------------

I just want to create a button that can download the file from the first table, the files was stored in a folder. I was little bit confused, why the developer staff before me that built this scripts (now the person was quit and I cannot contact him) add to three tables for uploaded files. And if I was changed the upload field, I have to changed everything.

Any clue?or link that can help me perhaps?to solve my confusedness.

Thank you for the helps from you guys here.

Sorry for my English. :)

  • 写回答

2条回答 默认 最新

  • douhui7136 2014-02-11 15:35
    关注

    I believe the query you're looking for is:

    SELECT t1.file_title, t1.file_name, t1.file_dir,
           t3.books_title, t3.books_author, t3.books_publisher
    FROM   first_table t1, second_table t2, third_table t3
    WHERE  t1.file_id=t2.file_id AND
           t2.books_id=t3.books_id
    

    This assumes the names of your tables are first_table, second_table, and third_table. Feel free to modify accordingly.

    To use this result in PHP, you could do something like this:

    $sql = "SELECT t1.file_title, t1.file_name, t1.file_dir, " .
           "       t3.books_title, t3.books_author, t3.books_publisher " .
           "FROM   first_table t1, second_table t2, third_table t3 " .
           "WHERE  t1.file_id=t2.file_id AND " .
           "       t2.books_id=t3.books_id";
    
    $query_result = mysqli_query($sql);
    
    $data = array();
    while ($row = mysqli_fetch_assoc($query_result)) {
      $row_data = array();
      foreach ($row as $key => $value) {
        $row_data[$key] = $value;
      }
      array_push($data, $row_data);
    }
    
    foreach($data as $item) {
      $path_to_file = $item['file_dir'] . '/' . $item['file_name'];
      print "<a href='$path_to_file'>" . 
              $item['books_title'] .
              ' (Author: ' . $item['books_author'] . ', ' .
              ' Publisher: ' . $item['books_publisher'] . ')</a>';
      print '<br>';
    }
    

    Of course, the outputting of HTML is entirely for demonstration purposes - I don't know exactly what kind of formatting you need. The critical pieces to understand are:

    • piece the $path_to_file together based on the $item['file_dir'] and $item['file_name']
    • make your link (or your button, or whatever you choose to use) point to that $path_to_file.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化