Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting 'ENUM' in MYSQLi [duplicate]

Tags:

php

enums

mysqli

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.

like image 502
Charles Avatar asked May 27 '26 22:05

Charles


1 Answers

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.

like image 118
Pupil Avatar answered Jun 01 '26 01:06

Pupil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!