PHP 未定义索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4842759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:03:10  来源:igfitidea点击:

PHP Undefined Index

phphtml

提问by dragonridingsorceress

This is going to sound really stupid, but I cannot figure out why I am getting this error.

这听起来真的很愚蠢,但我不明白为什么我会收到这个错误。

I have created a selection box, named "query_age" in my html form:

我在我的 html 表单中创建了一个名为“query_age”的选择框:

<form method="get" action="user_list.php">
<select name="query_age">
  <option value="">Doesn't matter</option>
  <option value="between 18 and 30">18 - 30</option>
  <option value="between 31 and 40">31 - 40</option>
  <option value="between 41 and 50">41 - 50</option>
  <option value="between 51 and 60">51 - 60</option>
  <option value="between 61 and 70">61 - 70</option>
  <option value="between 71 and 80">71 - 80</option>
  <option value="between 81 and 90">81 - 90</option>
  <option value="> 90">Older than 90</option>
</select>

In the corresponding php form, I have:

在相应的php表单中,我有:

$query_age = $_GET['query_age'];

When I run the page, I get this error:

当我运行页面时,我收到此错误:

Notice: Undefined index: query_age in index.php on line 19

注意:未定义索引:第 19 行 index.php 中的 query_age

I don't understand why this is happening, and I'd love to know how to make it go away.

我不明白为什么会发生这种情况,我很想知道如何让它消失。

回答by Radek Benkel

I don't see php file, but that could be that -
replace in your php file:

我没有看到 php 文件,但这可能是 -
在你的 php 文件中替换:

$query_age = $_GET['query_age'];

with:

和:

$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);

Most probably, at first time you running your script without ?query_age=[something]and $_GEThas no key like query_age.

最可能的是,在你第一次没有运行脚本?query_age=[something],并$_GET有一个像没有密钥query_age

回答by alex

The checking of the presence of the member before assigning it is, in my opinion, quite ugly.

在我看来,在分配成员之前检查成员是否存在是非常丑陋的。

Kohana has a useful functionto make selecting parameters simple.

Kohana 有一个有用的功能,可以使选择参数变得简单。

You can make your own like so...

你可以像这样制作自己的...

function arrayGet($array, $key, $default = NULL)
{
    return isset($array[$key]) ? $array[$key] : $default;
}

And then do something like...

然后做一些像......

$page = arrayGet($_GET, 'p', 1);

回答by Jason

The first time you run the page, the query_age index doesn't exist because it hasn't been sent over from the form.

第一次运行页面时,query_age 索引不存在,因为它没有从表单发送过来。

When you submit the form it will then exist, and it won't complain about it.

当您提交表单时,它就会存在,并且不会抱怨它。

#so change
$_GET['query_age'];
#to:
(!empty($_GET['query_age']) ? $_GET['query_age'] : null);

回答by NSDestr0yer

if you use isset like the answer posted already by singles, just make sure there is a bracket at the end like so:
$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);

如果您像单身人士已经发布的答案一样使用isset,请确保末尾有一个括号,如下所示:
$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);