0

嘿,在我的博客上,我想创建博客文章的标题,并让它可以点击到一个只有该文章的新页面。当我尝试它时,虽然我得到了错误:

注意:未定义索引:第 264 行 C:\wamp\www\blog_test\index.php 中的 post_id 调用堆栈 #TimeMemoryFunctionLocation 10.0009322712{main}( )..\index.php:0 ">Fanboy-ism

这是第 264 行:

<h3><a href="post.php?post_id=<?php echo $row_getDisplay['post_id']; ?>"><?php echo $row_getDisplay['title']; ?></a></h3>

有谁知道如何解决它,这让我感到困惑。请&&谢谢

编辑:

服务器行为的代码如下:

$maxRows_getArchives = 10;
$pageNum_getArchives = 0;
if (isset($_GET['pageNum_getArchives'])) {
  $pageNum_getArchives = $_GET['pageNum_getArchives'];
}
$startRow_getArchives = $pageNum_getArchives * $maxRows_getArchives;

mysql_select_db($database_blog, $blog);
$query_getArchives = "SELECT DISTINCT DATE_FORMAT(news.updated, '%M %Y') AS archive, DATE_FORMAT(news.updated, '%Y-%m') AS link FROM news ORDER BY news.updated DESC";
$query_limit_getArchives = sprintf("%s LIMIT %d, %d", $query_getArchives, $startRow_getArchives, $maxRows_getArchives);
$getArchives = mysql_query($query_limit_getArchives, $blog) or die(mysql_error());
$row_getArchives = mysql_fetch_assoc($getArchives);

if (isset($_GET['totalRows_getArchives'])) {
  $totalRows_getArchives = $_GET['totalRows_getArchives'];
} else {
  $all_getArchives = mysql_query($query_getArchives);
  $totalRows_getArchives = mysql_num_rows($all_getArchives);
}
$totalPages_getArchives = ceil($totalRows_getArchives/$maxRows_getArchives)-1;

mysql_select_db($database_blog, $blog);
$query_getRecent = "SELECT news.post_id, news.title FROM news ORDER BY news.post_id DESC LIMIT 10";
$getRecent = mysql_query($query_getRecent, $blog) or die(mysql_error());
$row_getRecent = mysql_fetch_assoc($getRecent);
$totalRows_getRecent = mysql_num_rows($getRecent);

$maxRows_getDisplay = 1;
$pageNum_getDisplay = 0;
if (isset($_GET['pageNum_getDisplay'])) {
  $pageNum_getDisplay = $_GET['pageNum_getDisplay'];
}
$startRow_getDisplay = $pageNum_getDisplay * $maxRows_getDisplay;

mysql_select_db($database_blog, $blog);
$query_getDisplay = "SELECT  news.post_id, news.title, news.pre,  DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC";
$query_limit_getDisplay = sprintf("%s LIMIT %d, %d", $query_getDisplay, $startRow_getDisplay, $maxRows_getDisplay);
$getDisplay = mysql_query($query_limit_getDisplay, $blog) or die(mysql_error());
$row_getDisplay = mysql_fetch_assoc($getDisplay);

if (isset($_GET['totalRows_getDisplay'])) {
  $totalRows_getDisplay = $_GET['totalRows_getDisplay'];
} else {
  $all_getDisplay = mysql_query($query_getDisplay);
  $totalRows_getDisplay = mysql_num_rows($all_getDisplay);
}
$totalPages_getDisplay = ceil($totalRows_getDisplay/$maxRows_getDisplay)-1;

mysql_select_db($database_blog, $blog);
$query_getPosts = "SELECT post_id, title, updated FROM news ORDER BY updated DESC";
$getPosts = mysql_query($query_getPosts, $blog) or die(mysql_error());
$row_getPosts = mysql_fetch_assoc($getPosts);
$totalRows_getPosts = mysql_num_rows($getPosts);

$queryString_getDisplay = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_getDisplay") == false && 
        stristr($param, "totalRows_getDisplay") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_getDisplay = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_getDisplay = sprintf("&totalRows_getDisplay=%d%s", $totalRows_getDisplay, $queryString_getDisplay);

$queryString_getArchives = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_getArchives") == false && 
        stristr($param, "totalRows_getArchives") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_getArchives = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_getArchives = sprintf("&totalRows_getArchives=%d%s", $totalRows_getArchives, $queryString_getArchives);



$var1_getDisplay2 = "-1";
if (isset($_GET['archive'])) {
  $var1_getDisplay2 = $_GET['archive'];
  $query_getDisplay = sprintf("SELECT news.title, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y'), news. AS formatted FROM news WHERE DATE_FORMAT(news.updated, '%%Y-%%m') = %s ORDER BY news.updated DESC", GetSQLValueString($var1_getDisplay2, "text"));
} elseif (isset($_GET['post_id'])) {
  $var2_getDisplay3 = $_GET['post_id'];
  $query_getDisplay = sprintf("SELECT news.title, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE news.post_id = %s", GetSQLValueString($var2_getDisplay3, "int"));
} else {
  $query_getDisplay = "SELECT news.title, news.blog_entry, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LIMIT ".$pageNum_getDisplay.", 5";
}
$getDisplay = mysql_query($query_getDisplay, $blog) or die(mysql_error());
$row_getDisplay = mysql_fetch_assoc($getDisplay);
$totalRows_getDisplay = mysql_num_rows($getDisplay);
?>
4

2 回答 2

1

我编辑了整个答案,

您没有在查询中选择 post_id:将查询更改为:

if (isset($_GET['archive'])) {
  $var1_getDisplay2 = $_GET['archive'];
  $query_getDisplay = sprintf("SELECT news.post_id, news.title, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y'), news. AS formatted FROM news WHERE DATE_FORMAT(news.updated, '%%Y-%%m') = %s ORDER BY news.updated DESC", GetSQLValueString($var1_getDisplay2, "text"));
} elseif (isset($_GET['post_id'])) {
  $var2_getDisplay3 = $_GET['post_id'];
  $query_getDisplay = sprintf("SELECT news.post_id, news.title, news.blog_entry, DATE_FORMAT(news.updated, '%%M %%e, %%Y') AS formatted FROM news WHERE news.post_id = %s", GetSQLValueString($var2_getDisplay3, "int"));
} else {
  $query_getDisplay = "SELECT news.post_id, news.title, news.blog_entry, DATE_FORMAT(news.updated, '%M %e, %Y') AS formatted FROM news ORDER BY news.updated DESC LIMIT ".$pageNum_getDisplay.", 5";
}

这还将在您的代码中选择 post_id 以供进一步参考,这可能会解决您的所有问题。(取决于您的数据库的外观)

添加了 news.post_id 用于从数据库中进行选择。我认为这是解决方案。

于 2013-01-14T12:17:25.477 回答
0

奇怪的是,此时尚未设置该变量。

但是您可以检查变量是否已设置并回显,如果没有。

例子:

echo (isset($row_getDisplay['post_id']) ? $row_getDisplay['post_id'] : '');

以下是如何在代码中实现它:

<h3><a href="post.php?post_id=<?php echo (isset($row_getDisplay['post_id']) ? $row_getDisplay['post_id'] : ''); ?>"><?php echo (isset($row_getDisplay['title']) ? $row_getDisplay['title'] : ''); ?></a></h3>
于 2013-01-14T12:17:58.350 回答