我已经在php mysql中创建了一个简单的测验应用程序。该应用程序分为两部分,一部分是给学生的,另一部分是给管理员的,所以在管理区域中,我创建了addquestion页面,这里是代码
<form class="form-horizontal " action="addquestion.php" method="post" style="width:50%;margin:0 auto" id="addquestionform">
<div class="form-group">
<label class="control-label col-sm-3" for="q">Question</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="q" placeholder="Enter Question" name="q">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="ch1">Choice 1</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="ch1" placeholder="Enter choice 1" name="ch1">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="ch2">Choice 2</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="ch2" placeholder="Enter Choice 2" name="ch2">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="ch3">Choice 3</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="ch3" placeholder="Enter choice 3" name="ch3">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="ch4">Choice 4</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="ch4" placeholder="Enter choice 4" name="ch4">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="cn">Correct Choice</label>
<div class="col-sm-9">
<input type="number" class="form-control" id="cc" placeholder="Enter correct choice" name="cc">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="cn">Choose Catogry</label>
<div class="col-sm-9">
<select class="pull-left form-control" name="cat">
<?php while($row = mysqli_fetch_assoc($category)){ ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['subject_name']; ?> </option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-10">
<button type="submit" class="btn btn-default">Add Question</button>
</div>
</div>
</form>
对于php部分,代码如下,我从表单中获取值,例如问题,选择1,选择2,选择3,选择4,正确选择以及将添加问题和选择的类别。
if($_SERVER["REQUEST_METHOD"] == "POST"){
$question = $_POST['q'];
$choices[1] = $_POST['ch1'];
$choices[2] = $_POST['ch2'];
$choices[3] = $_POST['ch3'];
$choices[4] = $_POST['ch4'];
$correct = $_POST['cc'];
$cat = $_POST['cat'];
$query = mysqli_query($con,"insert into subject_questions values('','$question','$cat')");
if($query){
$questionid = mysqli_insert_id($con);
echo $questionid;
foreach($choices as $key => $value){
if($key == $correct){
$correct = 1;
}else{
$correct = 0;
}
$insert = mysqli_query($con,"insert into objectives values('','".$correct."','".$value."','".$questionid."')") or die(mysqli_error());
}
if($insert){
echo " <script> alert('question added successfully'); </script> ";
}else{
echo " <script> alert('question not added'); </script> ";
}
}
}
$category = mysqli_query($con,"select * from subject_category");
现在的问题是,当问题和选项被添加到数据库中时,如果第一个选项正确,则仅将第一个选项选择为正确,例如,如果其他某个选项是正确的,则不会将1作为正确的选项放置在那里,因此只能用于第一个选择我猜问题出在下面的部分中,在这里我使用了每个循环来循环遍历来自表单的选择,然后将其存储在$ choices数组中,因此我检查键是否与在表单中输入的正确值匹配,如果如果它是正确的,那么正确的变量将为1;如果不是,则它将为0。因此,如前所述,如果第一个选择正确,它只会得到1,而对于其他选择则不会。
foreach($choices as $key => $value){
if($key == $correct){
$correct = 1;
}else{
$correct = 0;
}
$insert = mysqli_query($con,"insert into objectives values('','".$correct."','".$value."','".$questionid."')") or die(mysqli_error());
}
答案 0 :(得分:1)
问题在于您正在重用变量名$correct
。首先,它包含正确答案的编号,但是随后您为其分配了1
或0
。在下一次迭代中,当您执行以下操作:
if ($key == $correct)
它不再保存正确答案的号码。
使用不同的变量。
foreach($choices as $key => $value){
if($key == $correct){
$is_correct = 1;
}else{
$is_correct = 0;
}
$insert = mysqli_query($con,"insert into objectives values('','".$is_correct."','".$value."','".$questionid."')") or die(mysqli_error());
}
答案 1 :(得分:0)
似乎您在循环内覆盖了$correct
的值。除1之外,没有正确答案;重新覆盖变量$correct
并重写代码。
代替写作
foreach($choices as $key => $value){
if($key == $correct){
$correct = 1;
}else{
$correct = 0;
}
$insert = mysqli_query($con,"insert into objectives values('','".$correct."','".$value."','".$questionid."')") or die(mysqli_error());
}
你可以写
foreach($choices as $key => $value){
if($key == $correct){
$insert = mysqli_query($con,"insert into objectives values('','1','".$value."','".$questionid."')") or die(mysqli_error());
}else{
$insert = mysqli_query($con,"insert into objectives values('','0','".$value."','".$questionid."')") or die(mysqli_error());
}
}
例如
如果您将$ correct的值设为1以外的值,则在第一次迭代$correct
重置为0
之后,在循环的其余部分中,其值均为0
。