2011-04-05 153 views
0

我也做了下面的代碼,我現在得到一個數據庫錯誤 - >似乎仍然無法得到它的上傳:上傳查詢笨

錯誤:

Column 'image_path' cannot be null 

Database Structure

控制器:

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

我所試圖做的是當表單有效時將數據保存到數據庫中ase(正常工作)並上傳圖像。

如果我掙扎的是,我無法獲取用戶文件「名稱」來保存和文件不會上傳。

這可以在索引函數內完成嗎?

+0

我不,但是沒有人幫助我最離開賽道 – 2011-04-05 18:26:06

回答

0

上傳的文件根本不包含在文章中,因此無法以您想要的方式獲取文件名。你必須做這樣的事情,而不是:

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」什麼的(見羅斯的回答僅供參考)

0

函數$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.

,你應該是好去。