Automatically trimming length of string submitted to MySQL

后端 未结 4 1760
南笙
南笙 2020-12-11 04:51

When I submit a form field to a mySQL database is there a way to set the database to automatically discard any data in excess of the data field length?

I know I can

4条回答
  •  佛祖请我去吃肉
    2020-12-11 05:36

    Just set the DB field size to the max size you want to accept.
    if you want max 20 chars, then set it to varchar(20) or char(20).
    Just be careful with the utf8 inputs, some of them characters takes 2 places instead of one.
    But, as you can read in comments. Choosing a utf8 charset will solve this.

    If you are speaking about white spaces...Then usually you are suppose to use only one place in your code to do the insert/update/delete queries. In this place you can easily apply programmaticlly any filter you want, and be sure it will be applied to all of your inserts.

提交回复
热议问题