CodeIgniter Forums

Full Version: Help Column cannot be null
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys i just need your help about my form having error submitting it

Column 'date' cannot be null

and here's my code:

PHP Code:
public function create_purchase(){

        
$data_list = array(

            
'sup_name' => $this->input->post('sup_name'),
            
'qty' => $this->input->post('qty'),
            
'price' => $this->input->post('price'),
            
'date_expired' => $this->input->post('date_expired'),
            
'date' => $this->input->post('date')
            );

        
$this->model_products->add_purchase($data_list);
        
$this->session->set_flashdata('success''Successfully Add!');
        
redirect('admin/purchase''refresh');
    } 


Model
PHP Code:
public function add_purchase($data_list){

        
        if(
$this->db->insert('purchases'$data_list)){
            return 
TRUE;
        }else{
            return 
FALSE;
        }
    } 

Database Table
PHP Code:
CREATE TABLE `purchases` (
`
purchase_idint(11NOT NULL AUTO_INCREMENT,
`
sup_namevarchar(50NOT NULL DEFAULT '',
`
datedate NOT NULL DEFAULT '',
`
qtyint(11NOT NULL DEFAULT '',
`
pricedecimal(12,2NOT NULL DEFAULT '',
`
date_expireddate NOT NULL DEFAULT '',

// Others 

Hope you can help me. Thanks!
CREATE TABLE `purchases` (
`purchase_id` int(11) NOT NULL AUTO_INCREMENT,
`sup_name` varchar(50) NOT NULL DEFAULT '',
`date` date NULL DEFAULT '',
`qty` int(11) NOT NULL DEFAULT '',
`price` decimal(12,2) NOT NULL DEFAULT '',
`date_expired` date NOT NULL DEFAULT '',

// Others
As Avenirer points out, you could change the definition of the column to accept null values. I'd add that your default values should be valid values of the columns' data types (e.g. date null default '0000-00-00' or int(11) not null default 0).

In your create_purchase() method, you should validate your data before passing it to $this->model_products->add_purchase(). If you don't want to use form validation (or don't want to generate an error for a null value on a field defined with 'not null' in your database), I would recommend creating a method in your model to perform some basic prep on the data before it is passed to the database methods, so you can make sure the data isn't going to generate database errors.