使用codegniter在表单中上传图片时遇到问题

时间:2016-08-23 22:49:08

标签: php mysql codeigniter codeigniter-2 codeigniter-3

尝试在表单中包含图像时,图像会正确添加,但数据库表中会添加两行。

尝试在表单中包含图像时,图像会正确添加,但数据库表中会添加两行。

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

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

我的控制器

<?php
$host = "127.0.0.1";
$username = "username";
$password = "password";
$database = "database";

$link = new mysqli($host, $username, $password, $database);

if($link->connect_error)
{
    die("Connection died: ".$link->connect_error);
}

$showtables = $link->query("SHOW TABLES;");

foreach($showtables->fetch_all() as $table)
{
    printf($table[0] . "\n");

    // what i'm trying to achieve:
    foreach(/* ??? */ as $column)
    {
        printf("\t- ".$column."\n");
    }
}
?>

我的模特

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'));
}

1 个答案:

答案 0 :(得分: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()