php 注意:未定义的偏移量:2 如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20606570/
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 03:20:27  来源:igfitidea点击:

Notice: Undefined Offset : 2 How to Solve?

php

提问by user3103739

I am having undefined offset:2 errors at Line 11 and 12. This is the code from Line 6 - 15.

我在第 11 和 12 行有未定义的偏移:2 错误。这是第 6 - 15 行的代码。

    if(isset($_POST['submit'])){

        $surveyID =$_POST['surveyCategory'];

        for($i=0; $i<count($_POST['id']); $i++){
            $questionID = $_POST['id'][$surveyID][$i];
            $answer = mysql_real_escape_string(htmlspecialchars($_POST['answer'][$surveyID][$i]));
            mysql_query("INSERT INTO answers(survey_id, question_id, answer_body) VALUES   ('$surveyID', '$questionID', '$answer')") or die (mysql_error());
        }
    }

回答by Jite

undefined offsetmeans that an array have ran out of bounds.
Ie, the array size is smaller than the index that you are trying to fetch an object from.

undefined offset意味着数组已超出范围。
即,数组大小小于您试图从中获取对象的索引。

From looking at your code, I would guess that its either the $surveyIDthat is higher than the array size ($_POST['id']array) or the $idthat is ($_POST['id'][$surveyID]array).
Are both of those arrays actually arrays?

通过查看您的代码,我猜想它要么$surveyID高于数组大小($_POST['id']数组),要么高于数组大小$id$_POST['id'][$surveyID]数组)。
这两个数组实际上都是数组吗?

You seem to iterate on the $_POST['id']array size, but you try to get the object from the $_POST['id'][$surveyID]array, this could be a issue, cause you never check the size of that array.
You probably want to iterate with: $_POST['id'][$i]or check the size of the array $_POST['id'][$surveyID]in the loop instead (would guess the later).

您似乎对$_POST['id']数组大小进行了迭代,但是您尝试从$_POST['id'][$surveyID]数组中获取对象,这可能是一个问题,因为您从不检查该数组的大小。
您可能想要迭代:$_POST['id'][$i]或者检查$_POST['id'][$surveyID]循环中数组的大小(稍后会猜测)。

I would recommend checking so that the $_POST['id']array have the $surveyIDkey set too (and the type you expect it to be), cause the POST parameter could be pretty much anything at all (client have full control over the post params, they can send any value they want to your script with those).

我建议检查$_POST['id']数组是否也$surveyID设置了键(以及您期望的类型),因为 POST 参数几乎可以是任何东西(客户端可以完全控制 post 参数,他们可以发送任何值他们想要你的脚本与那些)。

edit
While I'm at it: Always validate/escape your Request parameters (in this case POST) before inserting them into a database.
You are currently using Mysql_*which is Deprecated (will be removed from php) and it could be a good idea to go over to either mysqlior PDO.
Both PDOand mysqlihave Prepared statements, which helps a lot with escaping data (or at least see to that data inserted to database does not do bad things with it, like sql-injections).
So please, take a look at those (check the php.net docs)!

编辑
当我在做的时候:POST在将它们插入数据库之前,始终验证/转义您的请求参数(在这种情况下)。
您当前使用的Mysql_*是已弃用的(将从 php 中删除),最好转到mysqliPDO.
无论PDOmysqli准备的语句,这帮助了很多有逃脱的数据(或者至少看到插入到数据库中的数据没有做不好的事情吧,就像SQL-注射)。
所以请看看那些(检查 php.net 文档)!

回答by Niet the Dark Absol

You are counting $_POST['id'], but iterating over $_POST['id'][$surveyID].

您正在counting $_POST['id'],但正在迭代$_POST['id'][$surveyID]

Change to $i < count($_POST['id'][$surveyID])

改成 $i < count($_POST['id'][$surveyID])

回答by Satish Sharma

error - undefined offset:2this problem occur due to when you are using a index which in not exists.

错误 -undefined offset:2当您使用不存在的索引时会出现此问题。

so problem in you code is that your post has not submitted with that index you are using.

所以你代码中的问题是你的帖子没有提交你正在使用的索引。

So print your $_POSTas follows and check it.

所以打印你的$_POST如下并检查它。

echo '<pre>';
print_r($_POST);

回答by John Allen

Try adding a print_r($_POST['id'])to see what this array element contains.

尝试添加 aprint_r($_POST['id'])以查看此数组元素包含的内容。

I'll bet its not a multi-dimensional array.

我敢打赌它不是多维数组。

回答by Pila

if(isset($_POST['submit'])) {
    $surveyID = $_POST['surveyCategory'];
    for($i=0; $i<count($_POST['id']); $i++) {
        if($i <> count($_POST['id'])) {
            $questionID = $_POST['id'][$surveyID][$i];
            $answer = mysql_real_escape_string(htmlspecialchars($_POST['answer'][$surveyID][$i]));
            mysql_query("INSERT INTO answers(survey_id, question_id, answer_body) VALUES   ('$surveyID', '$questionID', '$answer')") or die (mysql_error());
        }
    }
}