This question already has answers here:
Closed last year.
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.
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.
change
INSERT INTO 'table'(field1,field2) VALUES ('?,?');
to
INSERT INTO 'table'(field1,field2) VALUES (?,?);
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.