Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
插入问题时,我的php测验应用程序不工作_Php_Html_Css_Mysql - Fatal编程技术网

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

插入问题时,我的php测验应用程序不工作,php,html,css,mysql,Php,Html,Css,Mysql,我用php mysql创建了一个简单的测验应用程序。这个应用程序有两部分,一部分是学生的,另一部分是管理员的,所以在管理员区域我创建了addquestion页面,这里是代码 <form class="form-horizontal " action="addquestion.php" method="post" style="width:50%;margin:0 auto" id="addquestionform"> <div class="form-group"&

我用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>

似乎在循环中重写$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。

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

您的脚本非常开放,甚至可以在MYSQLI_uu或pdoapi中使用s@mplungjan这是管理区,这里也很重要吗?@RidaBatool这在任何地方都是很好的实践。如果问题或答案中有撇号,您的代码将不起作用。@barmar u r right您为什么不在答案中这么说?一个好的答案应该解释问题,以及你是如何解决的,而不仅仅是邮政编码。哦,我明白了。非常感谢你@barmar
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());
}