Answer the question
In order to leave comments, you need to log in
Do I need to validate the field length?
For example, the user's nickname in the mysql table is stored as a varchar field. The length of varchar, as far as I remember, is 255 bytes.
Should I check that the length of the nickname is no more than 255 characters (let's say that the size of each character is 1 byte) and display a message to the user that "the length of the nickname is no more than 255 characters"?
Or do not pay attention to the length and save the field to the database, and in case of an error (by the way, mysql will give an error if you try to write more data in the field than it is possible?) display a message to the user that "your request could not be completed"?
If I should check, then do I need to do it at all for every text field? For example, the size of a bigtext field?
Answer the question
In order to leave comments, you need to log in
Of course, you need to do it for the user, if it says "Your request could not be completed", he will think that the site is not working, and if "nickname length is not more than 255 characters", then it will be much clearer for him!
> By the way, mysql will give an error if you try to write more data in the field than you can?
No, mysql will not throw an error (UPD from JhaoDa: if strict mode (STRICT_TRANS_TABLES) is disabled). It will just cut the string to the length you want, in your case up to 255 characters.
So the answer is yes, you need to check for the length of the string, otherwise you will simply lose data if you exceed 255 characters.
JS check what the user enters, error handling will be much more beautiful, but in php I would also do the check.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question