2013-12-23 52 views
0

我正在向三個表中添加數據,我需要獲得第二個表中使用的第一個表的最後一個ID,該函數成功使用$this->db->insert_id()函數,Trying與第二張桌子仍然給我第一張桌子的ID。我的代碼安排是:如何在多次插入後從表中獲取最後一個ID

function addcrm() { 

    //Post data collection array from the webform form 
    $customerdata = array(

        "salutation"=>$this->input->post('salutation'), 
        "mobilenumber"=>$this->input->post('mobilenumber'), 
        "emailaddress"=>$this->input->post('emailaddress') 

    ); 
    $this->db->insert('api_customer', $customerdata); 

    $customer=$this->db->insert_id(); 
    $leaddata = array(
        "fieldrep"=>$this->input->post('fieldrep'), 
        "fk_customerID"=>$customer, 

        "te"=>$this->input->post('takage'), 
        "othercost"=>$this->input->post('othercost')      

    ); 

    $this->db->insert('api_lead', $leaddata); 



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

for ($i =0; $i<count($_POST['w_product']); $i++){ 
    $productdata = array(
     "name" => $_POST['w_product'][$i], 
     "type" => $_POST['w_type'][$i], 
      "cost" => $_POST['w_cost'][$i], 
     "fk_leadID"=> $leadID 
     ); 
      $this->db->insert('api_prod',$productdata); 

} 
    $url = base_url('cXXXXXXXXXXXXXX); 
    redirect($url); 

    } 
+0

缺少'()'解決了你的問題? '$ leadID = $ this-> db-> insert_id;' - >'$ leadID = $ this-> db-> insert_id();' – Kyslik

回答

0

你在第二次調用中顯然缺少某些東西。 ;)

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

$leadIdD = $this->db->insert_id; 

請參閱? :)

+0

這是什麼意見是... – Kyslik

-1

如果您使用自動增量表,您可以嘗試使用MYSQL_LAST_INSERT_ID(); http://dev.mysql.com/doc/refman/5.0/en/information-functions.html

+0

閱讀更多carefull,那不是重點。 –

+0

這很可能是'$ this-> db-> insert_id'已經做了什麼。另外,它會突然將網站編碼爲「mysql」。 –

+0

我建議你會嘗試本地方法,所以如果它不起作用,你可能會忽略這個答案 –

0

嘗試用下面的方法工作:

$this->db->start_cache(); // Before query 
$this->db->stop_cache(); // After query 
$this->db->flush_cache(); // Clear query 

這樣,你清楚並刷新查詢。

相關問題