Upload Query Codeigniter

Hello,

I am getting a database error and can’t get it to upload:

Error:

 Column 'image_path' cannot be null 

Database

Controller:


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


    }

Model:


    	function addSale($data) {
		
	$this->db->insert('sales', $data);
	return;
	}	


I think the controller logic may have gotten away from you a bit, first off, it’s set up so your form is located at:
yoursite.com/addsale

and if I went to:
yoursite.com/addsale/do_upload - It would set those options

Setting that aside:
There’s no reason to even be declaring that within a function, I would change your controller to something like this:

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) { 
    
    //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'; 

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

    }  

If you still receive the same error, after the line you declare $file_info on, do a:


echo "<pre>";
die(print_r($file_info));

Thanks for that, I posted this question on stackoverflow and that was the answer I got - I no it was away from the controller.

I have done the print_r and it echoed 1

Jess

After applying the changes I suggested that’s what you get?

I got the same error and then when I applied the print_r it gave me a 1

Oh right, after skimming the documentation at:
File Uploading Class : CodeIgniter User Guide

do_upload performs the actual upload, and retrieving the configuration options can be done by using:

$this->upload->data()

So simply change:

$file_info = $this->upload->do_upload();

To:


$this->upload->do_upload();
$file_info = $this->upload->data();

Thank you,

I have been trying those two lines for days. The file now uploads but the image name is not being saved to the database - still showing a 0 value in the column

At least we got one of the two issues sorted.

So not being saved is clearly an issue with AddSale() in the model.

I would first do a die(print_r()) of $data before you perform the addsale() method, and see if it returns your array full of data. If it does, then do the same thing in the actual model and see if it’s making its way across there.

It does show in the addsale()

I am unsure how to do it in the model -> do I do it the same way?

You would do it the same way, though if the array is proper in the controller just before it gets sent to the model, it’s highly unlikely it’s getting “lost in translation”.

So it has to be an actual configuration error…I’m assuming it’s one of the following:
Field(s) in your array don’t match the column names in the database.
Table name isn’t right/doesn’t exist.

The other possibilities, (classes/helpers not loaded) and query failures would most likely throw errors.

Resolved - I had to change the db type - was set too int as default

Thanks alot,

I will post other question up soon.

Just a quick question,

I have now uploaded the code to a server, now the images will not upload,

The print_r $file_info shows the correct path but the file will not show in the destination why?

Any error message when you upload? My best guess would be permissions of the folder they’re being uploaded to, if it’s only going to be images then you can CHMOD to 777.

Nope its going through fine, I have chmod it to 777 via ftp and the cpanel file manager. its strange maybe an ownership issue?

This issue is now resolved,

I have made another upload issue post here