0

在你问之前,是的,在问这个问题之前,我已经搜索了近 20 个关于 SO 的不同“未定义索引”问题。可悲的是,他们都没有给我解决问题的线索。我有一个简单的 index.php 文件,它根据您按下的链接切换不同的 html 文件。在一个似乎完全没有问题的实时服务器上,但是当我使用 Xampp 时,我收到一长串“未定义的索引”,我不知道为什么。我的 Index.php 文件如下。未定义索引消息指出“每行中的未定义索引 rv”。

<?php
include ("anime_header.html");

if($_GET['rv'] == "amnesia")         include ("Amnesia.html");
else if($_GET['rv'] == "shingeki")   include ("AttackOnTitan.html");
else if($_GET['rv'] == "chuunibyou") include ("Chuunibyou.html");
else if($_GET['rv'] == "crimeedge")  include ("CrimeEdge.html");
else if($_GET['rv'] == "datealive")  include ("DateALive.html");
else if($_GET['rv'] == "duskmaiden") include ("DuskMaiden.html");
else if($_GET['rv'] == "gargantia")  include ("Gargantia.html");
else if($_GET['rv'] == "K_anime")    include ("K_Anime.html");
else if($_GET['rv'] == "karneval")   include ("Karneval.html");
else if($_GET['rv'] == "kotoura")    include ("Kotoura-San.html");
else if($_GET['rv'] == "kaibutsu")   include ("LittleMonster.html");
else if($_GET['rv'] == "nerawareta") include ("Nerewareta.html");
else if($_GET['rv'] == "redgarden")  include ("RedGarden.html");
else if($_GET['rv'] == "saikano")    include ("Saikano.html");
else if($_GET['rv'] == "sakurasou")  include ("Sakurasou.html");
else if($_GET['rv'] == "sasamisan")  include ("Sasami-San.html");
else if($_GET['rv'] == "vividred")   include ("Vividred.html");
else include ("animereviews.html");

include ("ReviewFooter.html");
?>

我不是在寻找一种方法来抑制错误消息,而是了解它为什么只出现在我的 Xampp 服务器上以及将来如何修复它。

4

2 回答 2

0

检查是否设置了 $_GET['rv']

<?php
$rv = $_GET['rv'];
if(isset($rv)){
    include ("anime_header.html");

    if($rv == "amnesia")         include ("Amnesia.html");
    else if($rv == "shingeki")   include ("AttackOnTitan.html");
    else if($rv == "chuunibyou") include ("Chuunibyou.html");
    else if($rv == "crimeedge")  include ("CrimeEdge.html");
    else if($rv == "datealive")  include ("DateALive.html");
    else if($rv == "duskmaiden") include ("DuskMaiden.html");
    else if($rv == "gargantia")  include ("Gargantia.html");
    else if($rv == "K_anime")    include ("K_Anime.html");
    else if($rv == "karneval")   include ("Karneval.html");
    else if($rv == "kotoura")    include ("Kotoura-San.html");
    else if($rv == "kaibutsu")   include ("LittleMonster.html");
    else if($rv == "nerawareta") include ("Nerewareta.html");
    else if($rv == "redgarden")  include ("RedGarden.html");
    else if($rv == "saikano")    include ("Saikano.html");
    else if($rv == "sakurasou")  include ("Sakurasou.html");
    else if($rv == "sasamisan")  include ("Sasami-San.html");
    else if($rv == "vividred")   include ("Vividred.html");
    else include ("animereviews.html");

    include ("ReviewFooter.html");
}
?>
于 2013-09-03T05:32:02.360 回答
0

当您尝试访问数组中不存在的键时会出现该错误。举个例子,让我们做一个简单的数组:

$myarr = array(
    'a' => 5
);

如果您尝试访问“a”键,它将运行而不会出现任何错误:

if ($myarr['a'] == 5){
    echo 'Perfect!';
}

但是,如果您尝试访问尚未设置的键,则会出现“未定义索引”错误:

if ($myarr['b'] == 6){
    echo 'The previous line should cause an error.';
}

为了解决这个问题,请确保在访问之前存在“rv”键。确保设置密钥的最常用功能是isset

if(!isset($_GET['rv'])){
    echo 'Oops, it seems like that key is not set!';
    exit;
}
于 2013-09-03T01:28:46.003 回答