2014-07-08 30 views
0

我需要從控制器獲得最後一個插入ID到ajax成功函數...... 我想定製產品,當用戶按添加到購物車阿賈克斯執行和設計將要插入數據庫.... 並在插入數據庫即時獲取最後插入ID .....插入... ,但需要最後插入ID ....當用戶按在隱藏字段添加到購物車按鈕...
這裏是我的Ajax代碼。如何獲得ajax返回id在隱藏變量....請求頁

$('#store').click(function(){ 

    $.ajax({          
     type: "POST",    
     url: ajax_url_store,  
     data: {action: 'store', views: JSON.stringify(thsirtDesigner.getProduct()) }, 
     success: function(data) { 
          if(parseInt(data) > 0) { 
          //successfully added..here i am getting last insert id... 
          // and i want to pass that in hidden variable..... 
          // on view page..... 
           } 
           else { 
             document.getElementById('succes-message').innerHTML = 'You Design has not Been                         Saved';    
           } 
         }, 
         error: function() { 
          //alert('some error has occured...'); 
         }, 
         start: function() { 
          //alert('ajax has been started...');  
         } 
      }); 
}); 

我的CI控制器

public function saveData(){ 
      if($this->input->post('action') == 'store') { 
       $views = $this->input->post('views'); 
       $id = $this->product_model->save($views); 
       $data = array('cust_id'=>$id); 
       $this->session->set_userdata($data);  
       if($id !=''){ 
        header('Content-Type: application/json'); 
        echo json_encode($id); 
        } 
       } 
      } 

回答

0

$這個 - > DB-> INSERT_ID()返回最後一個ID。

0

模型的插入操作後,使用下面的代碼

$這個 - > DB-> INSERT_ID();

這會給你id最後一次插入完成。

Documentation