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

标签 php html css mysql

我在 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/

相关文章:

php - 领英 API + PHP : request token remains blank

php - 显示列表中每个帖子的标签,而不使用太多查询

javascript - 触发事件时不打印图像

html - 拒绝 BeautifulSoup 中的一些 HTML 标签

jquery - 在页面加载时运行 css div 转换

css - 如何提供 API 来更改封装的 angularJS 指令(可重复使用的小部件/组件)的 CSS/样式/白色标签

php - 在干净的字符串函数之间添加空格

php - false 时继续执行代码

具有两个条件的 C# FindElement(By...)

html - 向右浮动的元素渲染得比其他元素高