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

标签 php codeigniter azure mysql-5.5 cleardb

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

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

Error Number: 1366

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

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

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

Controller :

$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 而不是 ''

关于php - 将默认值为空的非空列保留为空白时出现 MySQL 错误 1366,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35871347/

相关文章:

php - MySQL中Select语句的默认上限是多少?

php - 运行代码时 MySQL 函数中的语法错误

php - 使用多种自然语言的最有效方法

Codeigniter数据库插入失败

azure - 如何跳过Azure数据工厂、复制数据工具中已复制的文件?

升级到 2.0 后,Azure CloudStorageAccount.SetConfigurationSettingPublisher() 无效

powershell - 在Powershell中运行Hive时出现错误: “Please connect to a valid Azure HDInsight cluster before calling this cmdlet”

php - session 代码在 php 中不起作用

php - 表在数据库中不存在,但确实存在

php - 如何同时更新3个表4个id字段?