将默认值为空的非空列保留为空时,会出现 MySQL 错误 1366


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

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

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

错误号:1366

整数值不正确:"表示第 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;

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

根据您的

描述,您已将"薪水"列设置为整数列,但''作为空字符串插入。

因此,您需要在"薪水"列中设置0而不是''