如何解决当我尝试将用户答案与数据库答案进行比较时,未定义的偏移量
answer.PHP
<?PHP
include '_db.PHP';
$right=0;
$wrong=0;
$no_answer=0;
$answer=$_POST;
$sql="select id,ans from `questions`";
$result = MysqLi_query($conn,$sql);
while($row=MysqLi_fetch_assoc($result)){
if($row['ans']==$_POST[$row['id']]){
$right++;
}
// elseif($_POST[$row['id']]=="no_attempt"){
// $no_answer++;
// }
// else{
// $wrong++;
// }
}
echo "right".$right."<br>";
echo "no answer".$no_answer."<br>";
echo "wrong".$wrong."<br>";
?>
我收到偏移量错误,我从另一个文件中获取了详细信息,在该文件中,我尝试检查他的用户输入的数据库答案以及下面随附的其他文件源代码 ques_show.PHP
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<Meta charset="utf-8">
<Meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-sm-2"></div>
<div class="col-sm-8">
<h2>Quiz</h2>
<p>Please select the correct option</p>
<?PHP
$i=1;
$id=$_POST['cat'];
include '_db.PHP';
$sql="SELECT * FROM `questions` where cat_id='$id' ";
$result = MysqLi_query($conn,$sql);
while($row=MysqLi_fetch_assoc($result)){?>
<form action="answer.PHP" method="post">
<table class="table table-bordered">
<thead>
<tr>
<th class="danger"><?PHP echo $i;?> <?PHP echo $row['question'];?></th>
</tr>
</thead>
<tbody>
<?PHP if(isset($row['ans1'])){?>
<tr class="info">
<td> 1  <input type="radio" value="0" name="<?PHP echo['id']; ?>" > <?PHP echo $row['ans1'];?> </td>
</tr>
<?PHP }?>
<?PHP if(isset($row['ans2'])){?>
<tr class="info">
<td> 2 <input type="radio" value="1" name="<?PHP echo['id']; ?>"> <?PHP echo $row['ans2'];?></td>
</tr>
<?PHP }?>
<?PHP if(isset($row['ans3'])){?>
<tr class="info">
<td> 3 <input type="radio" value="2" name="<?PHP echo $row['id']; ?>" /> <?PHP echo $row['ans3'];?></td>
</tr>
<?PHP }?>
<?PHP if(isset($row['ans4'])){?>
<tr class="info">
<td> 4 <input type="radio" value="3" name="<?PHP echo['id']; ?>" > <?PHP echo $row['ans4'];?></td>
</tr>
<?PHP }?>
<tr class="info">
<td><input type="radio" checked="checked" style="display:none" value="no_attempt" name="<?PHP echo $row['id']; ?>" /></td>
</tr>
</tbody>
</table>
<?PHP $i++; } ?>
<center><input type="submit" value="Submit Quiz" class="btn btn-success" /></center>
</form>
</div>
<div class="col-sm-2"></div>
</div>
</body>
</html>
ques_show.PHP文件发布的详细信息用于answer.PHP。我为什么得到
注意:未定义的偏移量:第13行的C:\ xampp \ htdocs \ quiz \ answer.PHP中的2
解决方法
您在name
输入中传递了答案,而您正试图从id
获得POST
输入。
<tr class="info">
<td> 3 <input type="radio" value="<?php echo $row['id']; ?>" name="ans_id" /> <?php echo $row['ans3'];?></td>
</tr>
您的代码应类似于: answer.php
<?php
include '_db.php';
$right=0;
$wrong=0;
$no_answer=0;
$answer_id=isset($_POST['ans_id']) ? $_POST['ans_id'] : 0;
$sql="select id,ans from `questions`";
$result = mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result)){
if($row['id']==$answer_id){
$right++;
}
}
echo "right".$right."<br>";
?>
注意:请确认$row['id']
是您的答案ID或问题ID。您需要在此处输入答案ID。如果是问题ID,则必须进行更改。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。