CodeIgniter Forums

Full Version: Column cannot be null
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
All was working fine until upgraded to CI3.

I have a form where users have the option to choose their category from the dropdown. For ex:
If they select Cars, a second dropdown populated from database showing input text : Fuel, mileage, year.
If they choose Home, Surface, rooms,...

Before all that, they have to provide their name, address, zipcode ....

So, when the form is submited, it throws an error:

Column 'fuel' cannot be null, even Cars wasn't selected.

PHP Code:
 

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

Class 
Biz extends CI_controller 
 

 
   function __construct() 
 
  
 
   parent::__construct(); 

 
  

 
  public function add() 

 
  
     
$this->load->helper('form'); 
    
$biz=$this->get_form_data(); 
 
         $this->load->library('form_validation');
$this->form_validation->set_error_delimiters('','');
$this->form_validation->set_rules('city_id''City''trim|required|intval|max_length[20]|callback_city_check'); 
$this->form_validation->set_rules('district_id''District''trim|intval|max_length[20]|callback_city_check'); 
$this->form_validation->set_rules('name''Name''trim|required|max_length[200]'); 
$this->form_validation->set_rules('addrs''Address''trim|required|max_length[200]'); 
$this->form_validation->set_rules('fuel''Fuel''trim|required|max_length[200]'); 
// And so on

if($this->form_validation->run()) {

$biz=$this->get_form_data();
$this->load->model('Bizs');
$bizid $this->Bizs->add($biz);
redirect('/biz/'.$bizid);


// Funtion get_form_data
 
private function get_form_data() 

 
     

 
           $biz=array( 

 
             'city_id'=>$this->input->post("city_id"), 
 
             'district_id'=>$this->input->post("district_id"), 
 
             'name'=>$this->input->post("name"), 
 
             'addrs'=>$this->input->post("addrs1"), 
 
             'catid'=>$this->input->post("catid_1"), 
 
             'catid_2'=>$this->input->post("catid_2"), 
 
             'phone'=>$this->input->post("tel"), 
 
             'about'=>$this->input->post("about"),

'fuel'=>$this->input->post("fuel"),
'mileage'=>$this->input->post("mileage"),
'year'=>$this->input->post("year"),
// And so on

        
); 

 
           return $biz


Model

PHP Code:
public function add($data,$returnid true


if(
$this->db->insert($this->table_name,$data)) 


    return 
$returnid $this->db->insert_id(): true

    } 
else 
 
return false


Table structure:
PHP Code:
CREATE TABLE `biz` (
`
idint(11NOT NULL AUTO_INCREMENT,
`
usernamevarchar(20NOT NULL DEFAULT '',
`
namevarchar(50NOT NULL DEFAULT '',
`
fuelvarchar(50NOT NULL DEFAULT '',
// So on 

Thanks
You did define the fuel field in the table not to be null. But when you create the $biz array, you are passing to the 'fuel' key a value from an input element called "about" (which, by the way, is assigned to other 3 keys...), but that element is nowhere to be found in the rules... Make sure you are passing the right form element value to the "fuel" key.
(02-20-2015, 01:56 PM)Avenirer Wrote: [ -> ]You did define the fuel field in the table not to be null. But when you create the $biz array, you are passing to the 'fuel' key a value from an input element called "about" (which, by the way, is assigned to other 3 keys...), but that element is nowhere to be found in the rules... Make sure you are passing the right form element value to the "fuel" key.

Sorry about, just edited the code. Copy and paste (Didn't pay attention).
This is weird, in CI3 when I check the output of this array, :
PHP Code:
$biz=$this->get_form_data(); 

var_dump('$biz');

array(
28) { ["city_id"]=> string(1"3" 
    
["district_id"]=> string(2"24" 
    
["addrs1"]=> string(13"2646 Gand Ave" 
    
["zipcode"]=> string(5"15400" 
    
["catid_1"]=> string(2"50" 
    
["catid_2"]=> string(2"56" 
    
["price"]=> string(0"" 
    
["tel"]=> string(0"" 
    
["title"]=> string(10
    
"Test title" ["about"]=> string(20"This is just testing" 
    
["fuel"]=> NULL ["kilo"]=> NULL ["carburant"]=> NULL 
 


But with CI 2.2.1 this part is different
PHP Code:
["fuel"]=> NULL ["kilo"]=> NULL ["carburant"]=> NULL 

Here is a full output,
PHP Code:
array(28) { ["city_id"]=> string(1"3" 
    
["district_id"]=> string(2"24" 
    
["title"]=> string(10"Test title" 
    
["addrs1"]=> string(13"2646 Gand Ave" 
    
["zipcode"]=> string(5"15400" 
    
["catid_1"]=> string(2"50" 
    
["catid_2"]=> string(2"56" 
    
["fuel"]=> bool(false) ["kilo"]=> bool(false) ["carburant"]=> bool(false
// So on 

CI 2.2.1
PHP Code:
["fuel"]=> bool(false) ["kilo"]=> bool(false) ["carburant"]=> bool(false

CI3
PHP Code:
["fuel"]=> NULL ["kilo"]=> NULL ["carburant"]=> NULL 

Is this related to CI3 database changes?