简体   繁体   中英

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.

And make a new database and table, with 3 columns: id, name, email.

Then, I try to insert the data using 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:

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

Is this bug on 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. I use CodeIgniter and MySQL, and I set the database using Adminer.

I created 8 columns: id, name, email, password, role_id, is_active, date_created, and date_modified.

数据库列

And then I try to insert only 1 column (name) with codeigniter, and it show no error.

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

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.

空白值

My question is why this can happen? And can't it show an error?

MySQL has default values. When you use codeignier, it will rewrite that insert statement to something similar to:

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.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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