7

好的,我的页面上出现上述错误,我是第一次使用 PHP 5.3,之前使用过此代码,但从未收到此通知,所以我希望得到一些指导。我已经查看了一些问题,但是当我尝试将解决方案应用于我的代码时,它似乎不起作用。

此代码是来自纸混搭的 PHP 分页脚本,因此如果我能找到答案,它可能会帮助其他也在使用此代码并遇到相同问题的人。

生成通知的代码是这样的 -

$page = mysql_escape_string($_GET['page']);
if($page){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }   

从我读过的内容来看,建议添加 isset 并将代码更改为如下所示,但是我仍然遇到相同的错误。

$page = mysql_real_escape_string($_GET['page']);
 if(!isset($page)){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }   

任何意见,将不胜感激。

谢谢

斯坦

4

7 回答 7

6

“未定义索引”错误基本上是在告诉您您尝试使用没有匹配元素的索引从数组中获取某些内容。

在这种情况下,问题出在您的isset()呼叫上方的行中。索引'page'未在您的$_GET数组中定义。所以你需要先检查是否$_GET['page']设置:

if (isset($_GET['page'])) {
  $page = mysql_real_escape_string($_GET['page']);
  // do more stuff
}
于 2012-09-17T23:08:42.517 回答
1

我遇到了一些问题..这是我得到的最好和最短的解决方案..

$page = (isset($_GET["page"]) ? $_GET["page"]:$config["per_page"] ='');

或者

$page = (isset($_GET['page'])) ? $_GET['page'] : 0;
于 2015-08-12T18:44:00.733 回答
0

错误在你的第一行做

$page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 0;
于 2012-09-17T23:08:27.843 回答
0

在您的代码中,您检查页面是否未设置,然后使用该页面,从而产生相同的通知。只需远程感叹号。试试这个:

$page = mysql_real_escape_string($_GET['page']);
 if(isset($page)){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
}
于 2012-09-17T23:09:03.130 回答
0

尽管我在您的代码中看不到它,但我愿意打赌您$_GET['page']的代码中有一个空变量。进行分页时,如果您使用<a href="somePage.php?page=4链接样式,则需要继续将变量附加到锚标记。

检查您的链接以确保您一直将其传递回去。

undefined index当您尝试访问不存在的数组元素时会出现错误消息- 在这种情况下,它几乎可以肯定是$_GET数组。

于 2012-09-17T23:09:06.480 回答
0

希望这会有所帮助

<?php
# default page
$default = 'home.php';

# set document root path
$base = $_SERVER['DOCUMENT_ROOT'].'/redirect/';

# list of all site pages + the id they will be called by
$pages = array('home' => 'home.php','about' => 'about.php','contact' => 'contact.php');
if (isset($_GET['page'])) {
  $page = mysql_real_escape_string($_GET['page']);
if(array_key_exists($_GET['page'], $pages))
{
foreach($pages as $pageid => $pagename) {
if($_GET['page'] == $pageid && file_exists($base.$pagename))
{
          /* if somebody's making a request for ?page=xxx and
          the page exists in the $pages array, we display it
          checking first it also exists as a page on the server */
          include $base.$pagename;
      }
   } // end foreach
}
else {
          /* if the page isn't listed in $pages, or there's no ?page=xxx request
          we show the default page, again we'll also just make sure it exists as a file
          on the server */
          if(file_exists($base.$default)) include $base.$default;
 }
} else {
    header ("Location: index.php?page=home");
}


?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<body>
<div style="height:50px;border:1px solid #000;margin-bottom:10px">

</div>

<div style="float:left;width:15%;border:1px solid #000">
 <ul style="margin-left:10px;list-style-type:none">
     <li><a href="index.php?page=home">Home</a></li>
     <li><a href="index.php?page=about">About</a></li>
     <li><a href="index.php?page=contact">Contact</a></li>
 </ul>
</div>

<div style="float:right;width:80%;border:1px solid #000">
     <div style="padding:4px">
     <!-- content here -->
     </div>
</div>
</body>
</html>
于 2015-10-14T09:11:04.977 回答
-1

你应该这样做

if(isset($_GET['page']){  
  $page = mysql_real_escape_string($_GET['page']);  
  if(!isset($page)){  
     $start = ($page - 1) * $limit;   
  }else{  
    $start = 0; 
  }   
}

Undefined index: page指一些array特定的错误。index在您的代码中,具有值page的数组$_GET应该是需要一些保护的代码的第一行。

于 2012-09-17T23:10:54.870 回答