Solution of php and mysql error Field 'column_name' doesn't have a default value

Field column_name doesn't have a default value

Some times when we use the remote database in our localhost server or localhost database in remote server we got the error "Field 'column_name' doesn't have a default value".

This error will generate when we try to insert the value in database where some columns does not have any value to insert.

After spending my whole day i got very simple solution for this error. We need to do the simple modification in our php code.

How to ignore Field 'column_name' doesn't have a default value for all tables in a database at once. Here are all the methods, But method 1 is worked for me.


Method 1. 

Copy and Paste this line of code to all files

mysqli_query($con, "SET SESSION sql_mode = 'ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'");  

OR

Copy and Paste this line of code to a single file (connect.php). This file will be used by all the other files.



Method 2.

MySQL have mostly in STRICT SQL mode. Try to execute SQL query SET GLOBAL sql_mode='' or edit our my.cnf / my.ini to make sure we are not setting STRICT_ALL_TABLES and/or STRICT_TRANS_TABLES. 



Method 3. 

1. Open the phpmyadmin page -> Go to Startup Variable -> Go to the Advance tab

2. Now find the SQL Mode and remove the STRICT_ALL_TABLES and/or STRICT_TRANS_TABLES and then Click on Apply Changes.

3. Restart the MySQL Server.

4. Restart the localhost.



Method 4.

The column should set as default value.

ALTER TABLE <table_name> CHANGE COLUMN <column_name> INT(11) NOT NULL DEFAULT 0;



Method 5.

We should edit the my.cnf file in the MySQL directory. Change the sql_mode at the bottom :

#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

sql_mode=NO_ENGINE_SUBSTITUTION

Save the file and restart Mysql and localhost.



Method 6.


Remove the STRICT_TRANS_TABLES by resetting it to null using command.

mysql> set @@sql_mode = '';


Still issue not solve. Send me the enquiry or comment here.




Comments

Post a Comment