CodeIgniter Forums

Full Version: Why data is being added in NOT NULL fields ? [SOLVED]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum


I am learning codeigniter through tutorials and a book. I have coded this simple application with one table.My all columns of that table are set to Not Null,so far i have put no validations but still i was expecting that if i try to leave my form fields empty and submit it , no data should be added to my table but surprisingly empty rows are being added to my table.
Here is the structure of my table.
CREATE TABLE `books` (
  `title` varchar(100) NOT NULL,
  `description` varchar(100) NOT NULL,
  `ISBN` varchar(20) NOT NULL,
  `code` varchar(10) NOT NULL,
  PRIMARY KEY (`id`)

My controller add funtion :

function add( )
                          $this->load->library('session') ;
                          $this->load->model('books_model') ;
                          $data = array(
                                        'title' => $_POST['title'] ,
                                        'description' => $_POST['description'] ,
                                        'ISBN' => $_POST['ISBN'] ,
                                        'code' => $_POST['code'] ,
                                      $books = $this->books_model->add_book($data) ;
                                        $this->session->set_flashdata('status' , ' Data added ok<br/><br/>') ;
                                        $this->session->set_flashdata('status' , 'Data was not added,please try again<br/><br/>') ;
                                          redirect('books/index' , 'refresh' ) ;

My Model add function :

function add_book($data)
                          $result = 0;
                          if( !empty($data) )
                             $result = $this->db->insert('books' , $data) ;
                              return $result;

El Forum

there are multiple issues with what you are doing and why it is not working the way you expect

if you insert data into a mysql table with fields that are not null, and you do no give a value for the not null fields, then mysql will supply its own default value

any text type fields, i.e. varchar, char, text, mediumtext, longtext etc will be given an empty string as the value
any numeric fields will be given 0 as the value

if you submit a form with nothing in text fields, then those fields do return empty strings as their values, rather than not returning any values at all
so your code will receive values for each of the fields, but the values will be empty string, i.e. ''

your code is taking the POST values for the 4 fields and adding them to the $data array with specified indexes
then in your model you are checking if the $data array is not empty
it will never be empty because you are assigning empty strings to specified indexes in the array. the indexes will ensure the array is never empty even if all the values of all of the indexes are empty.

also there is a difference in both mysql and php between an empty string and a null value.

El Forum

Thanks a lot tonanbarbarian for explaining everything so nicely , php *magic* arrays always give me hard times. The things you told are not usually taught in classes.Please also tell me some solution if there is any, other than validations.Though i have implemented CI validations now.

El Forum

the solution would be to only add the post data to the $data array if the values are not empty strings
that way the $data array can be truely empty

if you use the $this->input->post() to retrieve the data from the post array it will return FALSE if there is no value which may help further, but there are other ways to do this as well