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. :)

图片转代码服务由CSDN问答提供 功能建议

我需要你们的帮助。

问题是我有 要显示从其他表连接的表中的下载链接,有三(3)个表。

  **第一个表:** 
 
file_id |  file_title |  file_name |  file_dir 
 ------------------------------------------- 
 |  |  | 
 
 
 **第二个表:** 
 
file_id |  books_id 
 ------------------- 
 
 
 **第三表:** 
 
books_id |  books_title |  books_author |  books_publisher 
 -----------------------------------------------  ------------ 
   
 
 

我只是想创建一个可以从第一个表下载文件的按钮,这些文件是存储的 在一个文件夹中。 我有点困惑,为什么我之前构建这个脚本的开发人员(现在这个人已经退出,我无法联系他)为上传的文件添加了三个表。 如果我更改了上传字段,我必须改变一切。

任何可以帮助我的线索或链接?解决我的困惑。

感谢你们这里的帮助。

对不起我的英文。 :)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.
    点赞 打赏 评论
  • dongyong8491 2014-02-11 15:10

    SELECT FirstTable.file_name, FirstTable.file_dir, ThirdTable.books_title, ThirdTable.books_author, ThirdTable.books_publisher INNER JOIN SecondTable ON FirstTable.file_id = SecondTable.file_id INNER JOIN ThirdTable ON SecondTable.books_id = ThirdTable.books_id

    INNER JOIN may not necessarily be the JOIN type you want to use, but this would be the general idea for grabbing data from 2 tables corresponding to a third (SecondTable) which links them.

    $link = $row['file_dir'] . $row['file_name'];

    点赞 打赏 评论

相关推荐 更多相似问题