我的php测验应用程序在插入问题时不工作

9o685dep  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(256)

我用php-mysql创建了一个简单的quizz应用程序。这个应用程序有两部分,一部分是为学生和其他部分是为管理,所以在管理区我已经创建了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());
}
vbopmzt1

vbopmzt11#

看来你在凌驾于 $correct 在回路内部。除1外,没有正确答案的;reomve重写变量 $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 .

ajsxfq5m

ajsxfq5m2#

问题是您正在重用变量名 $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());
}

相关问题