开发者

How to include ' in the input form?

开发者 https://www.devze.com 2023-01-07 06:10 出处:网络
I just realize that in my forms I couldn\'t save name like O\'Brian (It would saved as O only and \'Brian will be truncated).

I just realize that in my forms I couldn't save name like O'Brian (It would saved as O only and 'Brian will be truncated).

I'm using grails 1.2.2 with mysql.

is there simple ways to allow ' to be inserted into db ? rather than modify each form and put h开发者_高级运维tml replacement for that char ?


If inserting into the database is the problem, then you can use parameterized queries. This is strongly recommended anyway, since it avoids possible security risks.

Imagine if instead of entering just a quote character, the user enters "Brian'; DROP TABLE data" into your form!


use the escape character, \

e.g. O\'Brian

See http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html

That said, most DB abstraction layers will allow you to use parameterized queries that do this for you


Grails and its database abstraction GORM should handle that for you, unless you are saving it yourself using some lower level API:s. See the documentation here.

You should not need to replace such characters yourself, so I suggest you have another look at your code and see if you can spot what might cause the problem. I hope you can find an easy solution, it shouldn't be hard with Grails :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消