duanshaiduhao2471 2011-11-22 10:07
浏览 182
已采纳

$ this-> db-> insert_id()似乎正在抹去我以前的insert_id变量值

Here is the code:

$the_question = $_POST['question'];
            $the_answer = $_POST['answer'];
            $dummy_num[] = $_POST['dummy_answer_1'];
            $dummy_num[] = $_POST['dummy_answer_2'];
            $dummy_num[] = $_POST['dummy_answer_3'];
            //Get Hidden Test ID and Q_order
            $test_id = $_POST['test_id'];
            $q_order = $_POST['q_order'];
            //Submit Question
            $data_submit_q = array (
                'type' => 1,
                'question' => $the_question,
                'done' => 1
            );
            $this->db->where('test_id', $test_id);
            $this->db->where('q_order', $q_order);
            $this->db->update('questions', $data_submit_q);
            $question_id = $this->db->insert_id();
            $time_created = date('Y-m-d H:i:s');
            //Submit Answer 
            $data_submit_a = array (
                'test_id' => $test_id,
                'question_id' => $question_id,
                'option' => $the_answer,
                'company_id' => $data['company']->id,
                'job_id' => $data['session_job_id'],
                'time_created' => $time_created
            );
            $this->db->insert('options', $data_submit_a);
            $answer_id = $this->db->insert_id();

            //Let question know that answer is right.
            $data_submit_qr = array (
                'answer_id' => $answer_id
            );

            $this->db->where('id', $question_id);
            $this->db->where('test_id', $test_id);
            $this->db->update('questions', $data_submit_qr);

Setting the answer id removes the value of the question id, then on updating the database the answer id has no value also. Even though it does right before.

  • 写回答

2条回答 默认 最新

  • dragon8837 2011-11-22 10:23
    关注

    The method $this->db->insert_id() retrieves the ID when performing database inserts (as the name hints).

    You're using it after an update, that's why your $question_id gives problems (I think it would be set to FALSE, but I don't know for sure what does that method return when called on the wrong context). WHen you do your last update you use this as a WHERE condition, and if it is not set...

    It's not that your second call to insert_id() wipes out the first, I suspect is more like the first one is already NOT SET (or FALSE)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作