Inserting 'ENUM' in MYSQLi [duplicate]

2020-04-19 20:12发布

I would like to know how can I insert an 'enum' type in a mysqli query. I mean, if the field type is string and I execute a query like this:

INSERT INTO 'table'(field1,field2) VALUES ('?,?'); $stmt->bind_param('ss',$value1,$value2); $stmt->execute();

Everything is ok, but if I change 'field2' to enum type :

CREATE TABLE IF NOT EXISTS table ( field1 varchar(20) NOT NULL, field2 ENUM('Administrator', 'User', 'Guest');

Then this query inserts the first field but the second one is empty. I have checked that the second field value is one of the ENUM type defined.

Thank you.

标签: php mysqli enums
3条回答
仙女界的扛把子
2楼-- · 2020-04-19 20:37

I found the problem, here it goes:

I was saving special char in the enum type (accent)

field2 ENUM('Administrator', 'Usér', 'Guest');

and when my query was trying to insert Usér it fails. So I changed table definition to this:

field2 ENUM('Administrator', 'Usér', 'Guest'); and now everything is working fine.

Thank you for your responses and time.

查看更多
冷血范
3楼-- · 2020-04-19 20:49

change

 INSERT INTO 'table'(field1,field2) VALUES ('?,?');

to

 INSERT INTO 'table'(field1,field2) VALUES (?,?);
查看更多
等我变得足够好
4楼-- · 2020-04-19 20:50

You have defined the options for ENUM.

Also, you are inserting values into it.

As far as you enter pre-defined values, in your case:

('Administrator', 'User', 'Guest')

You can insert ENUM field as if it were a string, it will not create a problem.

If you enter any value other than the ones defined e.g. Administrator, ... the database will cause error.

查看更多
登录 后发表回答