2013-01-14 130 views
0

在我的博客上嘿,我想创建博客帖子的标题,并让它可以点击一个新的页面,只需在该博文上点击即可。当我尝试它,虽然我得到的错误:注意:未定义的索引:post_id

Notice: Undefined index: post_id in C:\wamp\www\blog_test\index.php on line 264 Call Stack #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); 
?> 
+0

好吧,你认为$ row_getDisplay [ 'POST_ID']是不是以前定义的? (正如错误所示:)) – povilasp

+0

当'var_dump($ row_getDisplay);'时,xdebug会返回什么? – Jelmer

+0

偏离主题:注意'mysql_xxx()'函数现在已被弃用;建议停止使用它们并切换到较新的mysqli或PDO扩展。 – SDC

回答

1

我editted全部答案,

你不选择你的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以供从数据库中进行选择。我认为这是解决方案。

+0

在代码中我会把这个放在哪里? – Robdogga55

+0

你的其他代码之上的一行。它必须先加载,然后再执行你的代码。 (在线263) –

+0

即使在我保存文档之前,它似乎给了我一个dreamweaver中的错误 – Robdogga55

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> 
+0

代码中的哪些地方我会这样做? – Robdogga55

+0

我在回答中添加了另一个示例。 – CharliePrynn

+0

它摆脱了错误,但post_id出现在url中没有,例如,当我点击标题,它会去“http://localhost/blog_test/post.php?post_id =” – Robdogga55