2016-05-11 28 views
0


在我的应用程序中,我有功能插入数据到mysqldb和一个新闻和数据库中有更多的一个图像我使上传表中的外部键新闻ID和所有插入工作正常,但问题新闻FK在上传表时,我插入数据插入取我将它的值与其他同时自动增量所以下次你发现第一行代码:
在上传表
upload table datacodeigniter自动增加foreignkey

插入的数据,你本身在正确的数据是行ID为31,另一个是自动增量
我插入的代码;
控制器

public function insertNews() { 
    $this->do_one_upload(); 
     $this->load->model('newsModel'); 
     $this->load->model('fileModel'); 
     $ad_ne_data = array(
       'titel' => $this->input->post('ad_news_title') , 
       'content' => $this->input->post('ad_news_content') , 
       'news_category_id' => $this->input->post('ad_news_category') , 
       'img_url' => $this->do_one_upload()[1]['full_path'], 
       'created_at' => date("Y-m-d") 
     ); 

     $this->newsModel->addNews($ad_ne_data); 
     $i=0; 
     while($i < count($this->do_one_upload())) { 
      // var_dump($this->do_one_upload()); 
      $ad_imgs_news =array(
       'title' => $this->do_one_upload()[$i]['client_name'], 
       'file_path' => $this->do_one_upload()[$i]['full_path'], 
       'type' => $this->do_one_upload()[$i]['file_type'], 
       'size' => $this->do_one_upload()[$i]['file_size'], 
       'img_news_id' => $this->newsModel->getLastNewsId() 
      ); 
      $i++; 
      $this->fileModel->addUpload($ad_imgs_news); 
      var_dump($ad_imgs_news); 
     } 
} 


新闻模型
在得到最后消息ID插入;

public function getLastNewsId() 
    { 
     $last_id = $this->db->insert_id(); 
     return $last_id; 
    } 

在上传模型

public function addUpload($data) 
    { 
     // $this->db->set('name', $name); 
     $this->db->insert('upload', $data); 
    } 


上传插图方法那么,这个问题是在分贝或代码或???
感谢,
问候

回答

1

尝试从

$this->newsModel->addNews($ad_ne_data); 

返回

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

获取INSERT_ID在addNews函数的查询之后。

+0

谢谢@arun它的工作很好 –

+0

愉快。总是乐于提供帮助。 – Arun