面对使用Codeigniter上载图像的问题

问题描述 投票:0回答:1

试图将图像包含在表单中,该图像已正确添加,但是在数据库表中添加了两行。

添加了数据的表的结构如下:

我的控制器

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();
}
php mysql codeigniter codeigniter-2 codeigniter-3
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.