drnmslpz42661 2016-05-11 17:28
浏览 15

PHP没有在MySQL表中插入多个数据

I am trying to build a multiple choice exam portal. It's working fine but when I am adding a question in my add.php file it get inserted but the choices of the question is not inserted in database

Here is my database:

Choices table:

choices table

Questions table:

questions table

Here is my code for add.php

<?php include 'includes/header.php'; ?>
<?php include 'config/config.php'; ?>
<?php include 'lib/Database.php'; ?>
<?php
$db = new Database();
if (isset($_POST['submit'])) {
    //Grab Post Data
    $question_number = $_POST['question_number'];
    $question_text = $_POST['question_text'];
    $correct_choice = $_POST['correct_choice'];
    $choices = array();
    $choices[1] = $_POST['choice1'];
    $choices[2] = $_POST['choice2'];
    $choices[3] = $_POST['choice3'];
    $choices[4] = $_POST['choice4'];

    //Insert question into database
    $query = "INSERT INTO `questions`(question_number, text) VALUES('$question_number','$question_text')";
    $insert_row = $db->insert($query);
    //validate
    if ($insert_row) {
        foreach ($choices as $choice => $value) {
            if ($value != '') {
                if ($correct_choice == $choice) {
                    $is_correct = 1;
                } else {
                    $is_correct = 0;
                }
                //Choice Query
                $query = "INSERT INTO `choices`(question_number,is_correct,text) VALUES('$question_number','$is_correct',
                             '$value')";
                //insert row
                $insert_row = $db->insert($query);
                if ($insert_row) {
                    continue;
                } else {
                    die($mysqli->error);
                }

            }
        }
    }
}
$query1 = "SELECT * FROM `questions`";
$result = $db->select($query1);
$total = $result->num_rows;
$next = $total + 1;
?>
 <form class="form-horizontal" action="signup.php" method="POST">
  <fieldset>
            <div id="legend">
              <legend class="text-center">Add Questions</legend>
            </div>
            <div class="control-group">
              <label class="control-label" for="username">Question Number</label>
               <div class="controls">
                <input name="question_number" value="<?php echo $next; ?>" placeholder="" class="form-control input-lg" type="number"/>
              </div>
            </div>
             <div class="control-group">
              <label class="control-label" for="text">Question Text</label>
              <div class="controls">
                <input name="text" placeholder="" class="form-control input-lg" type="text">
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="choice1">#Choice 1</label>
              <div class="controls">
                <input  name="choice1" placeholder="" class="form-control input-lg" type="text">
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="username">#Choice 2</label>
              <div class="controls">
                <input id="choice2" name="choice2" placeholder="" class="form-control input-lg" type="text">
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="username">#Choice 3</label>
              <div class="controls">
                <input id="choice3" name="choice3" placeholder="" class="form-control input-lg" type="text">
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="username">#Choice 4</label>
              <div class="controls">
                <input id="username" name="choice4" placeholder="" class="form-control input-lg" type="text">
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="username">Correct Choice Number</label>
               <div class="controls">
                <input id="username" name="correct_choice" placeholder="" class="form-control input-lg" type="number"/>
              </div>
            </div>
            <input type="submit" name="submit" class="btn btn-block btn-primary" value="Submit" class="submit"/>
  </fieldset>         
 </form>
<?php include 'includes/footer.php';?>

Now only the question is adding in the database but not the choices.

  • 写回答

1条回答 默认 最新

  • duanputian5341 2016-05-11 17:40
    关注

    The syntax appears to be correct, but you can delete simple quotes around numbers. In addition, it's better to make one request to insert all choices.

    //validate
    if ($insert_row) {
        $values = [];
        foreach ($choices as $choice => $value) {
            if ($value == '')
                continue;
            $is_correct = $correct_choice == $choice ? 1 : 0;
            $values[] = "($question_number, $is_correct, '$value')";
        }
        if (count($values) > 0)
        {
            $query = "INSERT INTO choices (question_number, is_correct, text) VALUES ".implode(',', $values);
            $insert_row = $db->insert($query);
            if (!$insert_row)
                die($mysqli->error);
        }
    }
    

    And your variables $value and $question_number come from $_POST variable, you have to use prepared statement to protect your query from SQL injection.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c