1

I am submitting data from an html form to a code igniter PHP backend, and null values get submitted to the database as zeros. Is there anyway to prevent this from happening (leave the database fields empty if the form field was empty?)

VIEW

<?php echo form_open('control_form/add_all'); ?>
<label for="f_membername">Member Name<span class="red">*</span></label>
<input type="text" name="f_membername"/>
<?php echo form_submit('submit', 'Submit'); ?>

CONTROLLER

function add_all(){
    $this->form_validation->set_rules('f_membername', 'Member Name', 'required');
    if ($this->form_validation->run() == TRUE)
    {
        #Add Member to Database
        $this->Model_form->add_all();
        $this->load->view('view_inc_header');
        $this->load->view('view_form_success');
        $this->load->view('view_inc_footer');
    }
}

MODEL

function add_all(){
    $v_membername = $this->input->post('f_membername');

    $data = array(

            'member_name' => $v_membername

    );

    $this->db->insert('members', $data);
}

The fieldtype in MySQL for most fields is tinytext, and the default value is "NONE"

4
  • What type is the database field in question, and does it have a default value? Commented Jun 7, 2011 at 15:53
  • I haven't done anything to specify a default value for any of the fields - all of my fields post as zeros if they're blank, whether they are text fields, numbers, serialized arrays, etc. Commented Jun 7, 2011 at 15:56
  • Can you show the coding part? Commented Jun 7, 2011 at 16:01
  • Ever find a solution to this? or what was causing the problem? Having the same problem. Commented Feb 11, 2013 at 3:04

3 Answers 3

2

Your empty form input fields are not NULL but empty strings '', so they are converted to 0 if the field type is INT. To insert NULL values just replace '' with NULL values in model/controller.

Sign up to request clarification or add additional context in comments.

1 Comment

But they show up as zero if they're text too. Is that normal?
0

I think the field type in your database is INT. If you insert null data into a INT field it will be converted to 0(zero).

1 Comment

the fieldtype is, in most cases, tinytext or varchar
0

Is any of your form validations running any usage of substr() ? i.e.:

substr(trim('SOME_FORM_VALUE'),0,7)

I was having a similar problem and it was definitely because substr() was returning FALSE or 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.