上传查询编码器



我已经完成了下面的代码,我现在得到一个数据库错误->仍然无法让它上传:

错误:

Column 'image_path' cannot be null

数据库结构

控制器:

class Addsale extends CI_Controller {
function __construct(){
parent::__construct();
}
function index() {
if(!$this->session->userdata('logged_in')) {
    redirect('admin/home');
}
// Main Page Data
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Add Sale';
$data['content'] = $this->load->view('admin/addsale', $data);
$this->load->view('admintemplate', $data);
//Set Validation
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|is_natural|required');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim|required');
$this->form_validation->set_rules('condition', 'Condition', 'trim|required');
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('price', 'Price', 'trim|required');
if($this->form_validation->run() === TRUE) {
$this->load->library('upload', $config);
$file_info = $this->upload->do_upload();
$data = array(  
    'name' => $this->input->post('name', TRUE),
    'location' => $this->input->post('location', TRUE),
    'bedrooms' => $this->input->post('bedrooms', TRUE),
    'bathrooms' => $this->input->post('bathrooms', TRUE),
    'condition' => $this->input->post('condition', TRUE),
    'description' => $this->input->post('description', TRUE),
    'price' => $this->input->post('price', TRUE),
    'image_path' => $file_info['full_path']
    );
$this->sales_model->addSale($data);
   }
}
function do_upload(){
    //Set File Settings
    $config['upload_path'] = './includes/uploads/';
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = '100';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';

}

}

模型:

    function addSale($data) {
$this->db->insert('sales', $data);
return;
}   
原始代码:

你好,

我有以下代码:

if($this->form_validation->run() === TRUE) {    
      $data = array(  
        'name' => $this->input->post('name', TRUE),
        'location' => $this->input->post('location', TRUE),
        'bedrooms' => $this->input->post('bedrooms', TRUE),
        'bathrooms' => $this->input->post('bathrooms', TRUE),
        'condition' => $this->input->post('condition', TRUE),
        'description' => $this->input->post('description', TRUE),
        'price' => $this->input->post('price', TRUE),
        'image_path' => $this->input->post('userfile', TRUE)
        );
    $this->load->library('upload', $config);
    $this->upload->do_upload(); 
       }
    }

我要做的是当表单有效时,将数据保存到数据库(工作正常)并上传图像。

我正在努力的是,我不能得到用户文件的"名称"来保存,文件将不会上传。

这可以在索引函数中完成吗?

上传的文件根本不会包含在post中,因此您无法以您想要的方式获得名称。你必须这样做:

if($this->form_validation->run() === TRUE) {    
    $this->load->library('upload', $config);
    $file_info = $this->upload->do_upload(); 
    $data = array(  
        'name' => $this->input->post('name', TRUE),
        'location' => $this->input->post('location', TRUE),
        'bedrooms' => $this->input->post('bedrooms', TRUE),
        'bathrooms' => $this->input->post('bathrooms', TRUE),
        'condition' => $this->input->post('condition', TRUE),
        'description' => $this->input->post('description', TRUE),
        'price' => $this->input->post('price', TRUE),
        'image_path' => $file_info['full_path']
    );
}

如果你只是想要文件名使用'client_name'或其他东西(参考Ross的回答),这将把完整路径放入数据库

函数$this->upload->do_upload()返回一个关于上传文件信息的数组。

:

$upload_data = $this->upload->do_upload();
print_r($upload_data); // to see everything
Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

因此,根据您需要的值,您将希望首先进行上传,如果成功,则继续处理POST数据。然后你可以在你的$data数组中这样做:

'image_path' => $upload_data['file_name']; // adjust value to get what you want.

你应该可以走了

最新更新