gpt4 book ai didi

php - 我的 php 测验应用程序在插入问题时无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:42 25 4
gpt4 key购买 nike

我在 php mysql 中创建了一个简单的测验应用程序。这个应用程序有两部分,一部分是给学生的,另一部分是给管理员的,所以在管理区域我创建了添加问题页面,这里是代码

  <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());
}

最佳答案

问题是您正在重用变量名 $correct。起初它包含正确答案的编号,但随后您将 10 分配给它。在下一次迭代中,当您执行以下操作时:

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());
}

关于php - 我的 php 测验应用程序在插入问题时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52812057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com