在两页内连接数据

I have two pages like a general blog articles have, 1 page as small preview of articles and 1 page with full article. Each small preview article has a button called "Read More" and when the user clicks it the all article opens in a separate page. I am uploading the articles with columns id, name, author, title, article and date. On page 1, the preview of all the article are retrieving from the database perfectly but the "READ MORE" button is not working perfectly. When clicked on "Read More" of any article preview, only the first article is showing up instead of different articles according to the previews. Here is the code
Page 1 (preview page)

<?php 
$db = mysqli_connect("localhost", "root", "", "myblogs.com");
$sql = "SELECT * FROM blogs";
$result = mysqli_query ($db, $sql);
while ($row = mysqli_fetch_array($result)){ 
  echo "        <div class='date-posts'>

<a href='index.php'>".$row['title']."</a></h1>
</div>
<div>".$row['author']."</div>

<div><img src='blogs/".$row['image']."'><div class='blogsampletext'>".$row['article']."</div>
<a href='page2.php.'>Read More</a>
</div>
";        
}
?>

Page 2 (where full article text is shown)

<?php

$db = mysqli_connect("localhost", "root", "", "myblogs.com");
$sql = "SELECT * FROM blogs LIMIT 1";
$result = mysqli_query ($db, $sql);
while ($row = mysqli_fetch_array($result)){
 echo "<tr><td colspan='2' align='center'><h2>".$row['title']."</h2><br>
 <img src='blogs/".$row['image']."'><br>
 <h4>".$row['author']."</h4>
 </td></tr>";
 echo "<tr><td colspan='2'><hr>".$row['article']."</td></tr>";


  }

?> 

Your link to the next page is incomplete: it doesn’t contain a reference to which article you want to read.

You don’t have any mention of a primary key, so, for this example, we’ll suppose that it’s called id.

Your link should take the form:

<a href='page2.php?id={$row['id']}'>

That will include the id as a query string.

Next, your second page should get the id and use it:

$id=intval($_GET['id']);
$sql = "SELECT * FROM blogs WHERE id=$id";

Normally you need to worry about SQL injection when using user data, but the intval function will take care of that.

You no longer need LIMIT, since a primary key is, by definition, unique.

Since you are relying on the user to follow the correct steps, you are leaving yourself a little exposed:

  • If the user somehow gets to the second page without going through the first, you need to allow for no input at all. You can do this by reading the first id as a default.
  • Since you are using GET as the method, an evil user can break things by adding id=nastystuff or something worse. In this case, since it is supposed to be an integer, intval will convert anything unknown to a 0.