drip5880 2018-03-24 12:45
浏览 68
已采纳

如何从MySQL表中的特定列获取值,然后将其显示在新页面上[重复]

This question already has an answer here:

So I have a simple question and I'm stuck with it. I have to use the same logic like in the provided code, but this time I have to make a new page - let's say "article.php", then I have to change the "Delete" button with "Show" button.

This is easy, but I can't do the following thing:

When a user clicks on the "SHOW" button, the link must redirect him into the "article.php" page, and after that to display ALL of the VALUES for the selected COLUMN. How is this going to happen? Using "foreach" cycle again or?... I really don't know how to do it. Any help will be highly appreciated!

<?php

include "app".DIRECTORY_SEPARATOR."config.php";
include "app".DIRECTORY_SEPARATOR."db-connection.php";

$foo_connection = db_connect($host, $user_name, $user_password, $database);

$sql = "SELECT * FROM articles";

$result = mysqli_query($foo_connection, $sql);

if(mysqli_num_rows($result) > 0){

    print "<table>
              <tr>
                <th>Article ID</th>
                <th>Article heading</th>
                <th>Article text</th>
                <th>Article author</th>
                <th>Article published</th>
                <th>Article delete</th>
              </tr>";
    foreach($result as $key => $cols){

        print "<tr>".
                  "<td>".$cols['id']."</td>".
                  "<td>".$cols['heading']."</td>".
                  "<td>".$cols['text']."</td>".
                  "<td>".$cols['author']."</td>".
                  "<td>".$cols['published']."</td>".
                  "<td><a href='app/sql/delete.php?id=".$cols['id']."'>Delete</a></td>".
              "</tr>";
    }
    print "</table>";
}
else {
    print "0 results";
}

mysqli_close($foo_connection);
</div>
  • 写回答

1条回答 默认 最新

  • duanpanzhu2910 2018-03-24 12:59
    关注

    In the new page, change the link from:

    <a href='app/sql/delete.php?id=".$cols['id']."'>Delete</a>
    

    To:

    <a href='app/sql/articledetails.php?id=".$cols['id']."'>Show</a>
    

    Then create a page articledetails.php that displays a single record based on the argument passed in id.

    Use a SQL statement that selects just the one row:

    $sql = "SELECT * FROM articlesWHERE id = ?";
    

    Then bind the $_GET['id'] value to the SQL statement and execute it. Then display the row as needed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示
  • ¥15 MATLAB有限差分法解一维边值问题
  • ¥200 内网渗透测试 横向渗透 Windows漏洞 Windows权限维持
  • ¥15 数据结构图的相关代码实现
  • ¥15 python中aiohttp.client_exceptions.ContentTypeError
  • ¥30 DeepLung肺结节检测生成最大froc值对应的epoch报错
  • ¥15 信号发生器如何将频率调大,步尽值改成10
  • ¥15 keil 5 编程智能家具,风扇台灯开关,人体感应等
  • ¥100 找一名渗透方面的专家