开发者

code igniter php null values get submitted to mysql DB as zeros (0)

开发者 https://www.devze.com 2023-03-10 20:27 出处:网络
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

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"


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.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消