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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!