MySQL 将具有默认值的非空列保留为空白时出现错误 1366

MySQL error 1366 appears when leaving non-null columns with default values as blank

我是 CodeIgniter 的新手,一般来说 PHP。我用某些非空列创建了一个新的 table。非空列都根据其数据类型设置了默认值,因此 VARCHAR 具有空字符串,而数值类型默认为 0。

然而,一旦我填写了表格(我故意将非空列留空以测试它们),并点击提交按钮,它就会出现以下错误:

Error Number: 1366

Incorrect integer value: '' for column 'salary' at row 1

当它发现用户没有输入任何值时,它为空字符串插入双引号。我检查了 mysql 模式,它变成了严格模式。但是,由于我使用的是多租户数据库 (Azure-clearDB),它们不允许我拥有超级权限,而且我无法关闭严格模式(或者我可以吗?)

有没有办法关闭此模式,或者有其他解决方法吗?我不想为每一列显式添加 SQL if-else 子句,因为那将是硬编码的。请帮忙 代码如下:

控制器:

$additional_data = array(
                'first_name'    => $this->input->post('first_name'),
                'last_name'     => $this->input->post('last_name'),
                'phone'         => $this->input->post('phone'),
                'salary'        => $this->input->post('salary'));

if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data))
        {
        $identity = $this->ion_auth->where('email', strtolower($this->input->post('email')))->users()->row();
        $forgotten = $this->ion_auth->forgotten_password($identity->{$this->config->item('identity', 'ion_auth')});
        $this->session->set_flashdata('message', $this->ion_auth->messages());
        redirect('auth/success', 'refresh');

型号:

    //filter out any data passed that doesnt have a matching column in the users table
    //and merge the set user data and the additional data
    $user_data = array_merge($this->_filter_data($this->tables['users'], $additional_data), $data);

    $this->trigger_events('extra_set');

    $this->db->insert($this->tables['users'], $user_data);

    $id = $this->db->insert_id();

    //add in groups array if it doesn't exits and stop adding into default group if default group ids are set
    if( isset($default_group->id) && empty($groups) )
    {
        $groups[] = $default_group->id;
    }

    if (!empty($groups))
    {
        //add to groups
        foreach ($groups as $group)
        {
            $this->add_to_group($group, $id);
        }
    }

    $this->trigger_events('post_register');

    return (isset($id)) ? $id : FALSE;

更新:我正在使用相同的插入语句插入多个列值。

根据您的描述,您已将 'salary' 列设置为整数列,但您将 '' 作为空字符串插入。

因此您需要在 'salary' 列中设置 0 而不是 ''