如何在单击“阅读更多”按钮时显示数据库的全部内容


How to display the full content of a database on clicking on 'Read More' button

我创建了一个名为Magazine的数据库,它有一个social_media表。该表有 5 列,即 ID、标题、作者、内容简短、内容。"内容短"列包含整篇文章的较版本(将显示在以下代码中),"内容"列包含整篇文章(单击"阅读更多"按钮时将显示)。我创建了一个主页,其中social_media的所有文章都显示在Content_Leftdiv中(注意:显示的是内容简短列而不是内容)。在数据库的每个条目的末尾都有一个"阅读更多"按钮。单击该阅读更多按钮后,我希望将用户重定向到新页面"文章.php",其中将显示相应文章的标题,作者和内容(内容的较长版本而不是内容简短)。我该怎么做?我创建了以下网页:

<?php require_once('connections/connection.php'); ?>
<?php
session_start();
$stmt = $con->query("SELECT * FROM social_media");
?>
<!doctype html>
<html>
<head>
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/menu.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<meta charset="utf-8">
<title>Home</title>
</head>
<body style="background-color:#E0DDDD">
<div id="Container">
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Social Media</a></li>
<li><a href="#">Tech</a></li>
<li><a href="#">Tips &amp; Tricks</a></li>
</ul>
</nav>
</div>
<div id="Content">
<div id="Content_Left">
  <h1><center>Social Media</center></h1>
  <table>
   <?php
   while($records=$stmt->fetch_assoc()) {
    $_SESSION["ID"] = $records['ID'];
    echo "<tr>";
    echo "<th><h3>".$records['Headline']."</h3></th>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>By <strong>".$records['Author']."</strong></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>".$records['Content_short']."  <a href='article.php?articleId=' " . $records['ID']. ">Read More...</a></td>";
    echo "</tr>";
   }
   ?>
  </table>
 </div>
 <div id="Content_Center">
  <h1>Header Here </h1>
  <p>Text Goes Here!</p>
 </div>
 <div id="Content_Right">
  <h1>Header Here </h1>
  <p>Text Goes Here!</p>
 </div>
 </div>
<div id="Footer">
<center>Your Copyright Message</center>
</div>
</div>
</body>
</html>

这是CSS文件(Layout.css):

body{
    margin:0;
    padding: 0;
}
#Container {
    width: 980px;
    height:auto;
    margin-left:auto;
    margin-right:auto;
    margin-top:20px;
    margin-bottom:21px;
}
#Header {
    height:120px;
    background-image:url(../Assets/Untitled-1.png);
    background-repeat:no-repeat;
    margin-bottom:21px;
}
#NavBar {
    height:60px;
    background-color:#000000;
}
#Content {  
    background-color:#FFFFFF;
    margin-top: 20px;
    padding: 5px;
    overflow:hidden;
}
#Content_Left {
    height: auto;
    width: 210px;
    padding:5px;
    float:left;
    background-color: lightblue
}
#Content_Center {
    height: auto;
    padding:5px;
    width: 500px;
    float:left;
    margin-left: 10px;
    margin-right: 10px;
    background-color: lightblue
}
#Content_Right {
    height: auto;
    padding:5px;
    width: 210px;
    float: right;
    background-color: lightblue
} 
#Container #Content h1 {
    font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
    font-style: normal;
    font-variant: normal;
    font-weight: bolder;
    text-shadow: 0px 0px;
}
#Footer {
    padding-top: 10px;
    height: 100px;
}

这是CSS文件(menu.css):

nav ul {
    margin:0;
    padding:0;
}
nav ul li {
    list-style-type:none;
    display:block;
    width:150px;
    height:60px;
    float:left;
    text-align:center;
    line-height:55px;
    font-family:Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", serif;
    font-size:17px;
}
nav ul li a {
    text-decoration:none;
    color:#FFF;
}
nav ul li:hover {
    background-color:#BBB5B5;
}
nav ul li:hover a {
    display:block;
    color:Black;
}

我想在新页面中显示该文章,但具有相同的背景主题。或者有没有其他方法可以显示整篇文章?我希望这么多细节就足够了。如果需要更多信息,请询问。

编辑

根据其他成员的建议,我已经编辑了代码。我的文章.php文件现在看起来像这样:

<?php require_once('connections/connection.php');?>
<?php
$articleId = $_GET['articleId'];
// you need to fetch only one record this time for showing only the article that wanted to be read. so use `where` condition
$stmt = $con->query("SELECT * FROM social_media WHERE id = $articleId ");
?>
<!doctype html>
<html>
<head>
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/menu.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<meta charset="utf-8">
<title>Home</title>
</head>
<body style="background-color:#E0DDDD">
<div id="Container">
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Social Media</a></li>
<li><a href="#">Tech</a></li>
<li><a href="#">Tips &amp; Tricks</a></li>
</ul>
</nav>
</div>
<div id="Content">
<div id="Content_Full">
  <table>
   <?php
   while($records=$stmt->fetch_assoc()) {
    echo "<tr>";
    echo "<th><h1>".$records['Headline']."</h1></th>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>By <strong>".$records['Author']."</strong></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>".$records['Content']."</td>";
    echo "</tr>";
   }
   ?>
  </table>
 </div>
 </div>
<div id="Footer">
<center>Your Copyright Message</center>
</div>
</div>
</body>
</html>

但是,我收到以下错误:

致命错误:调用非对象上的成员函数 fetch_assoc()

如何解决此问题?

改变一些东西

在 PHP 文件中

echo "<td>".$records['Content_short']."  <a href='article.php'>Read More...</a></td>";

echo "<td>".$records['Content_short']."  <a href='article.php?articleId= " . $records['ID']. "'>Read More...</a></td>";

然后在您的文章中.php

<?php
if(isset($_GET['articleId'])){
    $articleId = $_GET['articleId'];
}else{
    echo "Invalid access"; die;
}
// you need to fetch only one record this time for showing only the article that wanted to be read. so use `where` condition
$stmt = $con->query("SELECT * FROM social_media WHERE id = $articleId ");

按照文章列表页面上的相同html代码进行操作article.php

更改以下行

echo "<td>".$records['Content_short']."  <a href='article.php'>Read More...</a></td>";

自:

echo "<td>".$records['Content_short']."  <a href='article.php?id=' " . $records['ID']. ">Read More...</a></td>";

现在,您必须按ID获取详细信息并相应地显示数据。