2011-04-05 7 views
0

私は以下のコードを行っていると私は今、データベースのエラーを取得する - >まだそれがアップロードするように見えることはできません。アップロードクエリCodeIgniterの

エラー:

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

こんにちは、

私は、コードの次のセクションを持っていますフォームが有効なとき、データをdatabに保存するase(うまく動作)と画像をアップロードします。

私が苦労していたのは、保存するユーザーファイル「名前」を取得できず、ファイルがアップロードされないということです。

これはインデックス関数内で実行できますか?

+0

私はいない、しかし、誰も私を助けなかったほとんどが道から離れていた –

答えて

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

あなただけのファイル名の使用「クライアント名」または何かをしたい場合、これはあなたのデータベースにフルパスを入れます(参照のためのロスの答えを参照)

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.

、あなたが行くように良いことがあります。