Welcome Guest, Not a member yet? Register   Sign In
Column cannot be null
#1

(This post was last modified: 02-20-2015, 02:04 PM by rocks.)

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
Reply


Messages In This Thread
Column cannot be null - by rocks - 02-20-2015, 01:00 PM
RE: Column cannot be null - by Avenirer - 02-20-2015, 01:56 PM
RE: Column cannot be null - by rocks - 02-20-2015, 02:05 PM
RE: Column cannot be null - by rocks - 02-23-2015, 12:09 PM



Theme © iAndrew 2016 - Forum software by © MyBB