简体   繁体   中英

MySQL error when submitting '' in an array

I have a PHP loop that's auto-generating an array to INSERT into a table. But I get this error:

Database query failed: Incorrect integer value: '' for column 'id' at row 1
Last SQL query: INSERT INTO users(id, email, password, first_name, last_name) VALUES ('', 'test@user.com', 'fljhdsfsd', 'John', 'Doe')

I've tried setting the id field as No Null and Null, but that didn't help.

Any ideas? Thanks in advance for your help.

If your id field is an auto-incremented column then you can simply omit it from the INSERT query.

INSERT INTO users(email, password, first_name, last_name) 
VALUES ('test@user.com', 'fljhdsfsd', 'John', 'Doe');

An id will be generated automatically.

Your Mysql is running in the strict mode. You have to pass the NULL if you have no value for that column, if the column is auto increment you can keep out this column from query. Once try the below query, I believe this will work.

INSERT INTO users(id, email, password, first_name, last_name) 
VALUES (NULL, 'test@user.com', 'fljhdsfsd', 'John', 'Doe')

If id field is auto incremented you can skip that field. If not then read error, it says "Incorrect integer value" and you are giving '' witch is string. In this case before every insert you should fetch max id from database and increment it, or use any other ID generation.

This is happened because your id field in database now contains only integer value. and blanck is not a integer value. Just change the data type on your database.

Just change your datatype id "integer" to "varchar", tick out the "auto increment" and put a length like "250" field in database, then run your query.

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