English 中文(简体)
这笔钱——>db->insert_id()似乎正在冲销我以前插入的“可变值”
原标题:$this->db->insert_id() seems to be wiping away my previous insert_id variables value

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.

最佳回答

方法$this->db->insert_id(在数据库中检索inserts(作为背名)。

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...

这不是说,你第二次呼吁插入“(id) wipes out the first,我更像第一个呼吁是NOT SET(或FALSE)。

问题回答

It seems that there is a bug with insert_id, you can try using:

$query = $this->db->query( SELECT LAST_INSERT_ID() );
  $row = $query->row_array();
  $lastInsertId = $row[ LAST_INSERT_ID() ];

希望会有助于





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签