【问题标题】:How to get values from specific column in MySQL table, then display it on new page [duplicate]如何从 MySQL 表中的特定列中获取值,然后在新页面上显示它[重复]
【发布时间】:2018-09-03 00:38:42
【问题描述】:

所以我有一个简单的问题,我坚持下去。我必须使用与提供的代码中相同的逻辑,但这次我必须创建一个新页面 - 比如说"article.php",然后我必须将"Delete" 按钮更改为"Show" 按钮。

这很容易,但我不能做以下事情:

当用户单击“显示”按钮时,链接必须将他重定向到“article.php”页面,然后显示所选列的所有值。 这将如何发生?再次使用"foreach" 循环还是?... 我真的不知道该怎么做。 任何帮助将不胜感激!

<?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);

【问题讨论】:

  • 使用$_GET['']或使用会话
  • 您好,Dean.. 请您说的更具体一点。 :)
  • 好的,创建一个article.php,然后通过同一页面上的$_GET['id']值显示该页面上的文章,因此url将是url/article.php?set=id,就像那样-
  • 好的,我会试试的,我的朋友。
  • 这不是重复,我的朋友,我必须使用相同的逻辑:).. 不同,可能有点,但它不同。

标签: php html mysql


【解决方案1】:

在新页面中,将链接更改为:

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

收件人:

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

然后创建一个页面articledetails.php,根据id 中传递的参数显示单个记录。

使用只选择一行的 SQL 语句:

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

然后将$_GET['id']值绑定到SQL语句并执行。然后根据需要显示该行。

【讨论】:

  • 太好了,我现在就试试!
猜你喜欢
  • 1970-01-01
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
相关资源
最近更新 更多