简体   繁体   English

数组(['1'] => 1)1注意:未定义的偏移量:第152行的C:\\ xampp \\ htdocs \\ HR \\ functions \\ functions_applicants.php中的1

[英]Array ( ['1'] => 1 ) 1 Notice: Undefined offset: 1 in C:\xampp\htdocs\HR\functions\functions_applicants.php on line 152 2

The code below is to display the quiz(questions and answers) 下面的代码用于显示测验(问题和答案)

When submitting, I am getting error: 提交时,出现错误:

"Array ( ['1'] => 1 ) 1
Notice: Undefined offset: 1 in C:\xampp\htdocs\HR\functions\functions_applicants.php on line 152
2". 



<form method="post" action="index.php?results">

<?php 

for($i=1; $i<27; $i++) {

$query = query("SELECT * FROM assess_questions WHERE question_id =$i");
confirm($query);

while($row = fetch_array($query)) {

?> 

<div>
<h4><?php echo $row['question']; ?></h4>

<input type="radio" name="quizcheck['<?php echo $row['question_id']; ?>']" 
value=1><?php echo $row['A']; ?><br>
<input type="radio" name="quizcheck['<?php echo  $row['question_id']; ?>']" 
value=2><?php echo $row['B']; ?><br>
<input type="radio" name="quizcheck['<?php echo  $row['question_id']; ?>']" 
value=3><?php echo $row['C']; ?><br>
<input type="radio" name="quizcheck['<?php echo  $row['question_id']; ?>']" 
value=4><?php echo $row['D']; ?><hr>
</div>

<?php 
}
}
?>

<input class="btn btn-info" type="submit" name="submit_answers" 
value="Next">

</form>

THIS IS THE FUNCTION TO CHECK FOR THE ANSWER. 这是检查答案的功能。 THIS IS WHERE IM GETTING THE ERROR FROM. 这是从中获取错误的位置。 ITS THE $i that's causing the error. 导致错误的$ i。

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

    $result = 0;
    $i = 1;
    $average = 0;

    $item = ($_POST['quizcheck']);
    print_r($item) ;

    $query = query("SELECT * FROM assess_questions");
    confirm($query);

    while($row = fetch_array($query)) {
      print_r($row['answer_id']);

      $checked = ($row['answer_id']) == $item[$i];

      if($checked) {

       $result++;

      }

      $i++;
     }

    }

you begin with $i = 1; 您以$i = 1;开始$i = 1; in which case you'll avoid $item[0]; 在这种情况下,您将避免$item[0]; (first position) and it will mismatch $checked = ($row['answer_id']) . (第一个位置),它将与$checked = ($row['answer_id'])不匹配。

start with $i=0; $i=0; as if there's only one answer $i[1] will not exists but $i[0]; 好像只有一个答案$i[1]不存在,但是$i[0];

****EDIT**** first check your query result for not being void: example, having this db connection function/method: **** EDIT ****首先检查您的查询结果是否不为空:例如,具有以下数据库连接功能/方法:

  <?php
    function connection(){
        try{
            //host, user, passwd, DB name)
            $mysqli = new mysqli($host, $user, $passwd, $dbName);
            return $mysqli;
        }catch (Exception $mysqlin){
            echo "Error establishing connection with ddbb ".$mysqlin->getMessage(); 
        }
    }
    ?>

And modifying your code: 并修改您的代码:

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

    $result = 0;
    //indexes must start at 0 unless you ensure that you don't need 0 value and your algorithm will not keep trying to get a value out of bounds / array offset
    $i = 0;
    $average = 0;

    //i assume that $_POST['quizcheck'] is an array, otherwise the $item[$i] will be an out of bounds, which match with your issue (array offset). But i ensured some parts of your structure below for you to learn:

    $item = ($_POST['quizcheck']);
    print_r($item) ;
    //you'll must prepare this statement after, for security
    $sql = query("SELECT * FROM assess_questions");
    //we get the result of this query on $result, if possible
    if($result = connection()->query($sql)){

    //we get the first row inside $rs (usually called Record Set) as associative (it means that you'll call the values as the column name on DB unless you use AS "Alias" on your query.
    $rs = mysqli_fetch_assoc($result);
   //if the record set is not void:
    while($rs!="") {
      //assuming that your DB col is called answer_id
      print_r($rs['answer_id']);
      //checked will get the value of matching those two arrays, so make sure you control nullables:
      if($checked = ($rs['answer_id']) == $item[$i]){
        //only if $checked value could be set 
           if($checked) {
               $result++;
           }
       }

      $i++;
     }
   //repeat this to get the next value, when it has no more values it will be void so it will escape from while loop.
   $rs = mysqli_fetch_assoc($result);
   }
 }

Never assume that you'll get always a value. 永远不要以为自己会永远得到价值。

Never assume that users will put numbers in some input only because you told them to do it. 永远不要假设用户只是因为您告诉用户输入数字而输入数字。

Never use dates without checking. 切勿使用未经检查的日期。

Never state an algorithm with not-controlled vars/function outputs. 切勿声明具有不受控制的var /函数输出的算法。

Control all data all over across your code and comment it, it will help you to avoid issues and modify your code months after you coded it. 控制整个代码中的所有数据并对其进行注释,这将帮助您避免问题,并在编码几个月后修改代码。

Hope it helps you. 希望对您有帮助。 I recommend you to get a hosting to test/code in a controlled environment. 我建议您在托管环境中获得托管以进行测试/编码。

The clue is in the contents of $item which you have done a print_r on and got the result: 线索在$item的内容中,您已经在其中进行了print_r并得到了结果:

Array(['1'] => 1)

You're getting this result because in your html your radio buttons are labelled quizcheck['n'] where n is the question id. 之所以得到这个结果,是因为在您的html中,单选按钮被标记为quizcheck['n'] ,其中n是问题ID。 So presumably in this case you have pressed the first radio button in the first question. 因此,大概在这种情况下,您已经按下了第一个问题中的第一个单选按钮。 You should change the line which gives the radio buttons a name to 您应该更改为单选按钮指定名称的行

<input type="radio" name="quizcheck[<?php echo $row['question_id']; ?>]" value=1><?php echo $row['A']; ?><br>

(ie remove the single quotes around <?php echo $row['question_id']; ?> ). (即删除<?php echo $row['question_id']; ?>周围的单引号)。 This will make $item look like: 这将使$item看起来像:

Array ( [1] => 1 )

so the test 所以测试

($row['answer_id']) == $item[$i];

will work. 将工作。 Note the parentheses around $row['answer_id'] are unnecessary. 请注意,不需要在$row['answer_id']周围加上括号。

The other issue you are going to run into is that your form obviously doesn't require the user to submit an answer to every question. 您将要遇到的另一个问题是您的表单显然不需要用户为每个问题提交答案。 This means that in your while loop you need to check whether the user has submitted an answer to be checked against the result. 这意味着在您的while循环中,您需要检查用户是否已提交答案以对照结果进行检查。 If you are not going to make it compulsory to answer all questions, you will need to put an array_key_exists check around the result checking code: 如果您不打算强制回答所有问题,则需要在结果检查代码周围放置array_key_exists检查:

if (array_key_exists($i, $item)) {
    $checked = $row['answer_id'] == $item[$i];
    if ($checked) {
        $result++;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 注意:未定义偏移量:1 in C:\\xampp\\htdocs\\index.php 第 5 行 - Notice: Undefined offset: 1 in C:\xampp\htdocs\index.php on line 5 注意:未定义的偏移量:第19行的C:\\ xampp \\ htdocs \\ h_php \\ addTimes.php中的1 - Notice: Undefined offset: 1 in C:\xampp\htdocs\h_php\addTimes.php on line 19 注意:第21行的C:\\ xampp \\ htdocs \\ isan \\ hasildata.php中的未定义偏移量:0 - Notice: Undefined offset: 0 in C:\xampp\htdocs\isan\hasildata.php on line 21 注意:Undefined offset: 2 in C:\\xampp\\htdocs\\test.php on line 20 如何解决 - Notice: Undefined offset: 2 in C:\xampp\htdocs\test.php on line 20 how to solve 注意:使用未定义的常量 DB_HOST - 在 C:\\xampp\\htdocs\\blog\\system\\functions.php 中第 31 行假定为“DB_HOST” - Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\xampp\htdocs\blog\system\functions.php on line 31 注意:尝试访问 C:\\xampp\\htdocs\\res\\index.php 中第 97 行名称类型为 null 的数组偏移量: - Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\res\index.php on line 97 name: PHP:注意:未定义的索引:第9行的C:\\ xampp \\ htdocs \\ vdab \\ cookies.php中的NameFilledIn - PHP : Notice: Undefined index: NameFilledIn in C:\xampp\htdocs\vdab\cookies.php on line 9 注意:未定义的索引:在线的C:\\ xampp \\ htdocs \\ cars \\ index1.php中的elegido - Notice: Undefined index: elegido in C:\xampp\htdocs\cars\index1.php on line 注意:第29行的C:\\ xampp \\ htdocs \\ joyce \\ projectcal.php中的未定义变量:value_1 - Notice: Undefined variable: value_1 in C:\xampp\htdocs\joyce\projectcal.php on line 29 注意:未定义的索引:第15行的C:\\ xampp \\ htdocs \\ Sites \\ ooplr \\ classes \\ Validate.php中的密码? - Notice: Undefined index: password in C:\xampp\htdocs\Sites\ooplr\classes\Validate.php on line 15?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM