2016-08-23 5 views
1

イメージをフォームに追加しようとしていますが、イメージは正しく追加されますが、データベーステーブルには2行追加されます。問題が発生しました。コードギターを使用してフォームに画像をアップロードします。

イメージをフォームに含めると、イメージは正しく追加されますが、データベーステーブルには2つの行が追加されます。追加されたデータを持つテーブルの

構造は以下である:

http://i.stack.imgur.com/pnPAp.png

マイコントローラ

public function addRecordToTable(){ 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('client_id' , 'client_id', 'required'); 
    $this->form_validation->set_rules('l_address' , 'location address', 'required|min_length[3]|max_length[50]'); 

    if ($this->form_validation->run() == false) { 
     $this->load->model('Clientaccount_model'); 
     $data['bids']=$this->Clientaccount_model->bids(); 
     $data['loads']=$this->Truckeraccount_model->loads(); 
     $this->load->view('footer'); 
    } else { 
     $array = array(
        'client_id' => $this->input->post('client_id'), 
        'l_address' => $this->input->post('l_address'), 
     ); 

     $record_id = $this->consignmentupload_model->addData('consignment', $array); 
     $this->uploadFiles($record_id); 
    } 
} 

public function uploadFiles($record_id){ 
    $config = array(
       'upload_path' => FCPATH . "/uploads/", 
       'allowed_types' => 'jpg|png|jpeg', 
       'overwrite'  => TRUE,      
    ); 

    $this->load->library('upload', $config); 
    $files = $_FILES['uploads']; 

    foreach ($files['name'] as $key => $filename) { 
     $_FILES['uploads[]']['name']  = $files['name'][$key]; 
     $_FILES['uploads[]']['type']  = $files['type'][$key]; 
     $_FILES['uploads[]']['tmp_name'] = $files['tmp_name'][$key]; 
     $_FILES['uploads[]']['error'] = $files['error'][$key]; 
     $_FILES['uploads[]']['size']  = $files['size'][$key]; 

     $config['file_name'] = $filename; 
     $this->upload->initialize($config); 

     if (isset($_FILES['uploads[]']['name']) && !empty($_FILES['uploads[]']['name'])) { 

      if (! $this->upload->do_upload('uploads[]')) { 
       $error = array('error' => $this->upload->display_errors()); 

      } else { 
       $uploads[] = $this->upload->data(); 
       $array = array(
        'record_id' => $record_id, 
        'filename' => $_FILES['uploads[]']['name'], 
        'size'  => $_FILES['uploads[]']['size'] 
       ); 
       $this->consignmentupload_model->addData('consignment', $array); 
      } 
     } 
    } 
    redirect(site_url('clientaccount_ctrl')); 
} 

マイモデル

public function addData($table, $array) 
{ 
    $this->db->insert($table, $array); 
    return $this->db->insert_id(); 
} 
+1

コード内でaddData()関数を2回呼び出すのはなぜですか? – msvairam

+0

ありがとう、それを行う良い方法は何ですか?私はテーブルの画像の詳細も追加する必要があるからです。画像と共にフォーム入力を送信するより良い方法が必要です – Cooper

答えて

0
after your first insert that is, 

$record_id = $this->consignmentupload_model->addData('consignment', $array); 


you have to update the same row with your image, 

so in uploadFiles instead of, $this->consignmentupload_model->addData('consignment', $array); 

make a new model function updateData() where you update the same row which was inserted earlier, pass the luggage_id to updateData() 
関連する問題