插入多行时,MySQL返回空结果集

时间:2018-09-04 13:12:01

标签: php mysql

enter image description here我是PHP和Mysql的新手。我正在尝试创建一个测验项目,其中测验中的多个问题将一次插入数据库。我该怎么办。我在这里搜索解决方案,并尝试使用foreach。我没有错误,但是没有上传 这是我的表格

<form class="quiz-questions">
    <h1>Title Of Quiz</h1>
    
    <?php 
        for($counter = 1; $counter <= $_SESSION['quiz_no']; $counter++)
        {
            echo "<div class='quiz-left'>";
            echo "<div>";
            echo "<label for='textarea' class='sn-quiz'>$counter</label><textarea name='question[]' class='textarea' id='example-three' placeholder='Enter the Question' cols='60' rows='3' ></textarea>";
            echo "</div>";
            echo "<div class='option-tag'>";
            echo "<input type='checkbox' name='op1[]' value='optionA'>";
            echo "<input type='text' name='optionA[]' placeholder='optionA'>";
            echo "</div>";
            echo "<div class='option-tag'>";
            echo "<input type='checkbox' name='op1[]' value='optionB'>";
            echo "<input type='text' name='optionB[]' placeholder='optionB'>";
            echo "</div>";
            echo "<div class='option-tag'>";
            echo "<input type='checkbox' name='op1[]' value='optionC'>";
            echo "<input type='text' name='optionC[]' placeholder='optionC'>";
            echo "</div>";
            echo "<div class='option-tag'>";
            echo "<input type='checkbox' name='op1[]' value='optionD'>";
            echo "<input type='text' name='optionD[]' placeholder='optionD'>";
            echo "</div>";
            echo "</div>";
        }
        ?>

    
    <input type="submit" name="insert_quiz" class="btn-primary btn btn-lg" placeholder="SUBMIT">
</form>

这是我的提交代码

<?php 
 if(isset($_POST['insert_quiz'])) {
     
     $quiz_add_id = $_SESSION['quiz_id'];
     $count = $_SESSION['quiz_no'];
    $i = 0;
    foreach ($_POST as $val) {
        $question = $_POST['question'][$i];
        $optionA = $_POST['optionA'][$i];
        $optionB = $_POST['optionB'][$i];
        $optionC = $_POST['optionC'][$i];
        $optionD = $_POST['optionD'][$i];
        $op1 = $_POST['op1'][$i];
        
        $query = "INSERT INTO quizzes (quiz_add_id, quiz_question, quiz_option_A, quiz_option_B, quiz_option_C, quiz_option_D, quiz_option_correct)";

        $query .= "VALUES ('{$quiz_add_id}', '{$question}', '{$optionA}','{$optionB}','{$optionC}','{$optionD}', '{$op1}')";

        $create_quiz_query = mysqli_query($connection,$query);
        $i++;
      } 
  if(!$create_quiz_query ){
      die('QUERY FAILED' . mysqli_error($connection));

}
     
 }
?>

0 个答案:

没有答案