[英]Notice: Undefined offset: 1 in C:\xampp\htdocs\index.php on line 5
[英]Array ( ['1'] => 1 ) 1 Notice: Undefined offset: 1 in C:\xampp\htdocs\HR\functions\functions_applicants.php on line 152 2
下面的代碼用於顯示測驗(問題和答案)
提交時,出現錯誤:
"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>
這是檢查答案的功能。 這是從中獲取錯誤的位置。 導致錯誤的$ 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++;
}
}
您以$i = 1;
開始$i = 1;
在這種情況下,您將避免$item[0];
(第一個位置),它將與$checked = ($row['answer_id'])
不匹配。
從$i=0;
好像只有一個答案$i[1]
不存在,但是$i[0];
**** 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();
}
}
?>
並修改您的代碼:
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);
}
}
永遠不要以為自己會永遠得到價值。
永遠不要假設用戶只是因為您告訴用戶輸入數字而輸入數字。
切勿使用未經檢查的日期。
切勿聲明具有不受控制的var /函數輸出的算法。
控制整個代碼中的所有數據並對其進行注釋,這將幫助您避免問題,並在編碼幾個月后修改代碼。
線索在$item
的內容中,您已經在其中進行了print_r
並得到了結果:
Array(['1'] => 1)
之所以得到這個結果,是因為在您的html中,單選按鈕被標記為quizcheck['n']
,其中n
是問題ID。 因此,大概在這種情況下,您已經按下了第一個問題中的第一個單選按鈕。 您應該更改為單選按鈕指定名稱的行
<input type="radio" name="quizcheck[<?php echo $row['question_id']; ?>]" value=1><?php echo $row['A']; ?><br>
(即刪除<?php echo $row['question_id']; ?>
周圍的單引號)。 這將使$item
看起來像:
Array ( [1] => 1 )
所以測試
($row['answer_id']) == $item[$i];
將工作。 請注意,不需要在$row['answer_id']
周圍加上括號。
您將要遇到的另一個問題是您的表單顯然不需要用戶為每個問題提交答案。 這意味着在您的while循環中,您需要檢查用戶是否已提交答案以對照結果進行檢查。 如果您不打算強制回答所有問題,則需要在結果檢查代碼周圍放置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.