mysql - 使用 Codeigniter 进行多项选择测验

标签 mysql sql codeigniter-3

我有两个用于这个项目的表。问题表和答案表。 我希望问题表中的 Question_id feild 值对于使用另一个字段名称 Question_id 插入答案表中的每个选项都相同

但每次我这样做时,我都会得到这些结果:

Questions Table

Answers Table

正如你所看到的,question_ids 不相同,这不是我想要的。

这是我的代码: 公共(public)函数 insert_ Correct_ans($answer1, $answer2, $answer3, $answer4) {

$id = $this->db->insert_id();

    $this->db->query("INSERT INTO answers(
                                            question_id, 
                                            answer, 
                                            correct) 
                                   VALUES(
                                   '$lastID',
                                   '$answer1', '1')");

    $this->db->query("INSERT INTO answers(
                                            question_id, 
                                            answer, 
                                            correct) 
                                   VALUES(
                                   '$lastID',
                                   '$answer2', '0')");

    $this->db->query("INSERT INTO 
                                    answers(
                                            question_id, 
                                            answer, 
                                            correct) 
                                   VALUES(
                                        '$lastID',
                                        '$answer3', '0')");

    $this->db->query("INSERT INTO 
                                    answers(
                                            question_id, 
                                            answer, 
                                            correct)
                                   VALUES(
                                   '$lastID',
                                   '$answer4', '0')");
}

最佳答案

在调用函数insert_ Correct_ans()的时候也传入问题id。所以然后复制下面的代码。而不是数据写入插入查询重复使数组和用户CI数据库插入函数。

              <?php
      public function insert_correct_ans($que_id,$answer1, $answer2, $answer3, $answer4)
           {
            $data=array(
              array(
                'question_id'=>$que_id,
                'answer'=>$answer1,
                'correct'=>'1'
                ),
              array(
                'question_id'=>$que_id,
                'answer'=>$answer2,
                'correct'=>'0'
                ),
              array(
                'question_id'=>$que_id,
                'answer'=>$answer3,
                'correct'=>'0'
                ),
             array(
                'question_id'=>$que_id,
                'answer'=>$answer4,
                'correct'=>'0'
                )
              );
            }
            $this->db->insert('answers',$data);
          ?>

关于mysql - 使用 Codeigniter 进行多项选择测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41087055/

相关文章:

MySQL,联接多对多关系查询

mysql - MySQL流量的Wireshark密码捕获

php - [PHP][MYSQL] 内存不足,无法打开此页面

php - 每 1 小时更新 MySQL 表中约 100 万行

mysql - 当没有匹配数据而不是返回0行时,有没有办法返回一个零列?

mysql - 尽管使用 EXPLAIN 所示的索引,但跨数据库查询的运行时间异常长

java - 如何使用 Glassfish 和 DataSource 访问 SQL 数据库

sql - 使用别名时 Apache Phoenix 上的 "Column reference ambiguous or duplicate names"

JavaScript 返回 false 但仍在提交

php - 在 PHPSpreadsheet 上循环后添加单元格