简体   繁体   English

为什么在 mysql 数据库中只插入一列不会在 codeigniter 中显示错误?

[英]Why inserting only one column to mysql database not show an error in codeigniter?

UPDATE:更新:

Because the answers already given, I make a testing with fresh installation of CodeIgniter 3.1.11.因为已经给出了答案,所以我使用全新安装的 CodeIgniter 3.1.11 进行了测试。

And make a new database and table, with 3 columns: id, name, email.并创建一个新的数据库和表,包含 3 列:id、name、email。

Then, I try to insert the data using mysql cli:然后,我尝试使用 mysql cli 插入数据:

INSERT INTO users (name) VALUES ('hikki bocchi')
--- result ---
ERROR 1364 (HY000): Field 'email' doesn't have a default value

Then, I try using CodeIgniter:然后,我尝试使用 CodeIgniter:

--- first code ---
$query = "INSERT INTO users (name) VALUES ('hikki bocchi')";
$this->db->query($query);
--- second code ---
$this->db->insert('users', ['name' => 'hikki bocchi']);

The result using first code or second code are the same:使用第一个代码或第二个代码的结果是相同的:

the data inserted to the database with name = 'hikki bocchi' and email = '' (blank).插入数据库的数据名称 = 'hikki bocchi' 和 email = ''(空白)。

Is this bug on CodeIgniter?这是 CodeIgniter 上的错误吗? Or this only happens on me?还是这只发生在我身上?

OLD:老的:

First I'm new to CodeIgniter and Database, so I want to get easy to understand answer.首先,我是 CodeIgniter 和数据库的新手,所以我想得到易于理解的答案。 I use CodeIgniter and MySQL, and I set the database using Adminer.我使用 CodeIgniter 和 MySQL,并使用 Adminer 设置数据库。

I created 8 columns: id, name, email, password, role_id, is_active, date_created, and date_modified.我创建了 8 列:id、name、email、password、role_id、is_active、date_created 和 date_modified。

数据库列

And then I try to insert only 1 column (name) with codeigniter, and it show no error.然后我尝试使用 codeigniter 仅插入 1 列(名称),它没有显示错误。

$this->db->insert('users', ['name' => 'Hikki Bocchi']);

My logic is, the value of column id, date_created, date_modified will created automatically, but the column name, email, password, role_id, is_active are not.我的逻辑是,列 id、date_created、date_modified 的值会自动创建,但列名、email、password、role_id、is_active 不是。

But why the data can inserted with only one column?但是为什么数据只能插入一列呢? Isn't this must be an error?这不应该是一个错误吗? The name value has inserted, and other columns value are blank or 0.名称值已插入,其他列值为空白或 0。

空白值

My question is why this can happen?我的问题是为什么会发生这种情况? And can't it show an error?它不能显示错误吗?

MySQL has default values. MySQL 有默认值。 When you use codeignier, it will rewrite that insert statement to something similar to:当您使用 codeignier 时,它会将该插入语句重写为类似于:

INSERT INTO `users` (name) VALUES ('Hikki Bocchi');

From that point on, it's entirely up to your database to determine what the default values are.从那时起,完全取决于您的数据库来确定默认值是什么。 Use:利用:

SHOW CREATE TABLE `users`;

to view what your current default values are.查看您当前的默认值是什么。 You can edit the schema, and change what the default values should be.您可以编辑架构,并更改默认值。

To prevent blank values to be inserted in some of the columns, you need to declare them as not null able, and ensure that they do not have a default value set up.为了防止在某些列中插入空白值,您需要将它们声明为not null ,并确保它们没有设置默认值。

The following command can be used to apply these settings (this will remove the default value if there is one, and also make the column not nullable):以下命令可用于应用这些设置(如果有默认值,这将删除默认值,并使列不可为空):

alter table users modify email varchar(255) not null;

Demo on DB Fiddle : DB Fiddle 上的演示

-- create a table with default values
create table users (
    id int not null primary key auto_increment, 
    name varchar(255) default '', 
    email varchar(255) default ''
);

-- "partial" insert is allowed
insert into users(name) values('foo');

-- apply new setting on column email
alter table users modify email varchar(255) not null;

-- "partial" insert is not allowed anymore
insert into users(name) values('bar')
-- error: Field 'email' doesn't have a default value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM