这是我的第一篇文章。我搜索了很多,但找不到我要找的东西。所以我有这个文件,它破坏了帖子的内容并有更多内容:
<?php
include('../root/admin/includes/connect.php');
$sql = "SELECT * FROM posts";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "\n";
echo truncate($row['Content'], "article.php", "article_id", $row['ID']);
}
//function to truncate text and show read more link
function truncate($mytext, $link, $var, $id) {
//Number of characters to show
$chars = 25;
$mytext = substr($mytext, 0, $chars);
$mytext = substr($mytext, 0, strrpos($mytext, ' '));
$mytext = $mytext . " <a href='$link?$var=$id'>read more...</a>";
return $mytext;
}
?>
现在我有了这个文件
<?php
include('includes/connect.php');
$id = isset($_GET['ID']);
$query = mysql_query("SELECT * FROM posts WHERE ID='$id'") or die(mysql_error());
if (mysql_num_rows($query) == 0) {
echo "<tr><td colspan=\"3\">No Posts Were Found</td></tr>";
} else {
while ($post = mysql_fetch_assoc($query)) {
echo "<div class='perigrama'><div class='titlos'><h2><a href='#'>" . $post['Title'] . "</a></h2></div><div class='infos'><p>Posted by <a href='#'>" . $post['Author'] . "</a> on March 10, 2011 | <a href='#'>Full article</a></p></div><div class='image'><img src='#' width='540' height='300'></div><div class='content'><p>" . $post['Content'] . "</p></div><div class='more'><p><a href='#'>Full details</a></p></div></div>";
}
}
?>
当我点击“阅读更多”时,我想根据此处的 ID 从数据库输出帖子:
<html>
<head>
<title></title>
</head>
<link rel="stylesheet" type="text/css" href="../test/testdivformascss.css">
<body>
<?php
include('functions1.php');
?>
</body>
</html>
当我点击“阅读更多”时,我得到的输出是这样的,它将我重定向到正确的页面 ID,但告诉我没有找到帖子。
如果您能告诉我如何做到这一点,我将很高兴看到它。提前致谢!
最佳答案
改变
$id=isset($_GET['ID']);
至
$id=isset($_GET['article_id']) ? (int)$_GET['article_id'] : 0;
$id = isset($_GET['ID']);
- 这会返回 bool 并且始终为 false,因为 $_GET
中没有 ID
您的链接类似于 $mytext = $mytext . " <a href='$link?$var=$id'>read more...</a>";
它由输出的截断函数解析
<a href='article.php?article_id=SOME_ID'>read more...</a>
所以你应该使用$_GET['article_id']
要获取此 id,您应该通过 (int) 或 intval()
将其转换为 int
还有两件事。
您的代码非常危险,您应该认真阅读并谷歌搜索一些涵盖SQL_INJECTION的主题
Mysql_* 函数已弃用,并将在将来删除。请改用PDO 或Mysqli。这些引擎有一些称为准备好的语句,并且通过参数绑定(bind),您将不必处理正确的转义。
关于php - 我如何获取从一个页面到另一个重定向页面的数组ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17489236/