douwei8672 2017-04-23 11:29
浏览 30
已采纳

在两页内连接数据

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>";


  }

?> 
  • 写回答

1条回答 默认 最新

  • ds0678 2017-04-23 11:46
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题