【问题标题】:php opening link duplicate contentphp打开链接重复内容
【发布时间】:2013-06-12 13:43:34
【问题描述】:

所以在我的博客页面上,我使用 substr 只显示一些文章。这个想法是,一旦有人单击其中一个标题,他们就会被发送到显示全文的页面。

当我单击链接时,我会在 url 中获得正确的标题,例如,我单击第 4 个博客条目,我将被重定向到 index.php?id=4。问题是此页面上的内容保持不变,没有什么变化。我如何获得它,所以当我单击该文章时,我将被定向到仅包含该文章的完整页面而没有其他文章。在此先感谢各位,这让我整天头疼。

<?php
$dbinfo = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
$result = mysql_query($dbinfo) or die(mysql_error());
$return = '<p><a href="index.php"> Go Back To Content Page</a></p>';


if(mysql_num_rows($result) !=0):
    while($row = mysql_fetch_assoc($result)){
    echo '<div id="roundedbox"><h2><a href="index.php?id=' . $row['blog_id'] . '">' .  $row['title'] . ' </a></h2>';
    echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
    echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more</div>";

}
else:
    echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>'; 
    echo $return;
    endif;  
?>

【问题讨论】:

  • 如果您有单独的 PHP 脚本来显示概述和单独的文章,这会容易得多。
  • 有什么想法为什么要这样做?

标签: php url blogs


【解决方案1】:

您在代码中的查询将返回表的所有记录,您想要的是根据 $_GET['id'] 中的 id 参数返回记录的查询。确保只使用整数来防止 sql 注入是个好主意。

从您的代码中,您需要有一个单独的查询来使用 $_GET['id'] 参数获取适当的记录,如下所示...

    //the parameter is set and it is an integer
        if(isset($_GET['id']) && is_int($_GET['id'])) {
            $blogId = (int)$_GET['id'];
            $query = "SELECT blog_id, title, date, body FROM content WHERE blog_id='$blogId'";
            // run query and get record data and output it

        } else {
            //code to return all records as list
            $dbinfo = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
            $result = mysql_query($dbinfo) or die(mysql_error());
            $return = '<p><a href="index.php"> Go Back To Content Page</a></p>';


           if(mysql_num_rows($result) !=0):
           while($row = mysql_fetch_assoc($result)){
           echo '<div id="roundedbox"><h2><a href="index.php?id=' . $row['blog_id'].$row['title'] . ' </a></h2>';
           echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
           echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more</div>";

           }
           else:
             echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>'; 
             echo $return;
           endif; 
          }

【讨论】:

  • 那么我是否保留我以前的代码并将其添加到我显示数据的索引页面?我现在有点困惑
  • 我已将您的代码插入到需要去的地方。如果您使用 var_dump 进行测试以确保您遇到正确的循环,它将对您有所帮助。
  • 我收到此错误解析错误:语法错误,意外的 T_BOOLEAN_AND,在第一行需要 ',' 或 ')'。我通过使用包含添加此内容不认为这会导致任何问题...?
  • 包含不会导致任何问题,一个好的近似方法是使用伪代码设置您的代码,并在使用时替换为有效代码。这样你会更容易理解程序逻辑。我已经更新了错误的答案
猜你喜欢
  • 1970-01-01
  • 2015-10-17
  • 2010-12-17
  • 2021-08-13
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 2021-10-04
  • 2016-04-23
相关资源
最近更新 更多