Insert error

Please help me…
I have error, when i do insert to database… but when i do it from localhost there is no problem

when i do this :

"INSERT INTO `user` (`user_id`, `email`, `username`, `password`, `game_id`) VALUES('', '$email', '$username', '$password', '$game_id')"

There is an error : Incorrect integer value: ‘’ for column ‘user_id’ at row 1

And when i do this :

"INSERT INTO `user` (`user_id`, `email`, `username`, `password`, `game_id`) VALUES(null, '$email', '$username', '$password', '$game_id')"

The error is : Data truncated for column ‘game_id’ at row 1

So what should i do ?
And btw sorry for my bad english…

Instead of an empty string try and put an integer.

What data type is the column set to? And what data type are you inserting?

Also, in the future please remember that this isn’t Stackoverflow.

4 Likes

First of all this type of doubts should be asked in coding forums like stackover kind of sites for better solutions!

You can get various scenarios solution there if you have any coding error.

If the string value is assigned to PHP script, that too the empty string is assigned meanly then you will get the error code Incorrect integer value.

Behind this scenario, the script which is running tries to update or insert the row with the entry integer may be with values assigning it as default ( 0 or Null, AUTO_INCREMENT) value.

When you load data from file to a MySQL table, you might run into this error: Data truncated for column ‘column_name’ at row # That error means the data is too large for the data type of the MySQL table column. So define length!
Note:
Your localhost SQL config ini file may be modified thats y It can run without any error!

2 Likes

Regarding your main question, I second what the others have already said:

If you have a column of type INT, it means you need to fill it with an integer value. If you then provide it with null or a string (even if the string value is a number), MySQL won’t accept it.

This might be due to a different database version or setting. “Strict SQL Mode” is a setting that comes to mind that could affect this. If you have this turned off on your local system, and it’s turned on the server, then your local database might accept invalid values but the server might not.

Given that there was a difference in behavior between local development and our servers, I do think it’s fair to consider that maybe the databases here are not working correctly.

3 Likes

I aware of this! That is y I mentioned about SQL modified config ini also in my reply!

1 Like

If you have a column type INT and has AUTO_INCREMENT you can put NULL and it will increment the integer.

2 Likes

But I hope like admin says below sometime its won’t work coz of server setting.

Really? I personally would just exclude it from the column list in the INSERT statement so you can be sure MySQL will take the default value.

After all, if you have a column that’s nullable, but has a non-NULL default value, then it does matter. If you then provide NULL, I expect MySQL to store NULL, not the default value. If NULL then means “default value”, how would you ever set that nullable column to NULL?

But this might be a difference caused by server settings. If MySQL is configured to be more lenient, it would make sense to take the default value if you provide a NULL value where NULL is not allowed. But that’s just MySQL fixing your query error for you, rather than the way you’re supposed to do things.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.