mysql - Codeigniter 将多个图像插入数据库并根据唯一 ID 检索它们

标签 mysql image codeigniter upload

我正在建立一个网站,让用户出售他们的汽车。用户在表单中填写必要的字段,其中一个必填字段正在上传该特定汽车的图像。我的代码一切正常,预计当用户上传多个图像时,我将有两列具有相似的值上传的图像和 carID 这使我很难检索图像。如果有人知道我正确存储这些图像并检索它们的更好方法,我将不胜感激。这是我的数据库结构:

CarID |想要 |使|型号|年份|公里 |传输 |位置|价格|手机|图片

这是我的 Controller :

 // Upload new  car
    function add()
    {
          $this->load->library('form_validation');
     $this->form_validation->set_rules('list_options', 'Options', 'trim|required|min_length[2]|xss_clean');
     $this->form_validation->set_rules('make', 'Make', 'trim|required|min_length[2]|xss_clean');
     $this->form_validation->set_rules('model', 'Model', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('year', 'Year', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('kms', 'kms', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('transmission', 'Transmissions', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('location', 'Location', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('price', 'Price', 'trim|required|min_length[2]|xss_clean');
     $this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|min_length[2]|xss_clean');

        $upload_conf = array(
            'upload_path'   => realpath('images/'),
            'allowed_types' => 'gif|jpg|png',
            'max_size'      => '30000',
            );

        $this->upload->initialize( $upload_conf );

        // Change $_FILES to new vars and loop them
        foreach($_FILES['userfile'] as $key=>$val)
        {
            $i = 1;
            foreach($val as $v)
            {
                $field_name = "file_".$i;
                $_FILES[$field_name][$key] = $v;
                $i++;   
            }
        }
        // Unset the useless one ;)
        unset($_FILES['userfile']);

        // Put each errors and upload data to an array
        $error = array();
        $success = array();

        // main action to upload each file
        foreach($_FILES as $field_name => $file)
        {
            if ( ! $this->upload->do_upload($field_name))
            {
                // if upload fail, grab error 
                $error['upload'][] = $this->upload->display_errors();
            }
            else
            {
                    // otherwise, put the upload datas here.
                    // if you want to use database, put insert query in this loop

                    $upload_data = $this->upload->data();
                     $data=array(
                                    'want_to'=>$this->input->post('list_options'),
                                    'make'=>$this->input->post('make'),
                                    'model'=>$this->input->post('model'),
                                    'year'=>$this->input->post('year'),
                                    'kms'=>$this->input->post('kms'),
                                    'transmission'=>$this->input->post('transmission'),
                                    'location'=>$this->input->post('location'),
                                    'price'=>str_replace(",", "",$this->input->post('price')),
                                    'mobile'=>$this->input->post('mobile'),
                                    'image'=>$upload_data['orig_name'],

                                );
                     $imagedata=array(
                    'imagePath'=>$upload_data['orig_name'],
                    'imageName'=>$upload_data['file_name'],

                        );

                 $this->car_model->add_car($data); 

                // set the resize config
                $resize_conf = array(
                    // it's something like "/full/path/to/the/image.jpg" maybe
                    'source_image'  => $upload_data['full_path'], 
                    // and it's "/full/path/to/the/" + "thumb_" + "image.jpg
                    // or you can use 'create_thumbs' => true option instead
                    'new_image'     => 'images/thumbs/',
                    'width'         => 200,
                    'height'        => 200
                    );

                $medium_conf = array(
                    // it's something like "/full/path/to/the/image.jpg" maybe
                    'source_image'  => $upload_data['full_path'], 
                    // and it's "/full/path/to/the/" + "thumb_" + "image.jpg
                    // or you can use 'create_thumbs' => true option instead
                    'new_image'     => 'images/medium/',
                    'width'         => 600,
                    'height'        => 600
                    );


                // initializing
                $this->image_lib->initialize($resize_conf);
                 //$this->image_lib->initialize($medium_conf);

                // do it!
                if ( ! $this->image_lib->resize())
                {
                    // if got fail.
                    $error['resize'][] = $this->image_lib->display_errors();
                }
                else
                {
                    // otherwise, put each upload data to an array.
                    $success[] = $upload_data;
                }

            }

        }

        // see what we get
        if(count($error > 0))
        {
            $data['error'] = $error;
        }
        else
        {
            $data['success'] = $upload_data;
        }


        redirect(base_url());
    }

这是我的模型

function add_car($data)
{
    $this->db->insert('cars',$data);

}

最佳答案

第一件事很简单,只需为图像创建一个表,并使用 carid 作为图像表中的外键。我家 carid 是 cars 表中的主键,对吗?如果没有的话应该是。

在插入汽车数据的模型中,插入后可以使用 $this->db->insert_id() 获取 carid;函数见下面的代码。

function add_car($data)
{
  $this->db->insert('cars',$data);
  return $this->db->insert_id(); // it returns primary key value it should be carid.
}

现在在上传图像后在 Controller 中只需使用 add_car() 的返回值将其插入图像文件夹中。

希望对你有帮助!!如果没有,我很乐意帮助您!!

关于mysql - Codeigniter 将多个图像插入数据库并根据唯一 ID 检索它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800648/

相关文章:

php - $_POST 是 PHP 中的空数据

mysql - 根据排名过滤

mysql - 我可以用 cratedb 替换 mysql 大表吗

html - angularjs 背景问题。背景和页脚之间的差距很大。图像调整大小不会改变任何东西

php - 如何使用代码点火器事件记录查询返回一维数组中的表字段

javascript - 使用 CodeIgniter 通过 AJAX 提交表单

传感器时间戳数据表中 MySQL 查询时间太长

mysql - 如何从两个表中获取不同的 ID 和数量总和

image - 裁剪图像并使用 ffmpeg 获取左侧部分

java - 如何在java中将图像文件转换为整数数组?