Page 1 of 1

saving chinese/non-ascii character from input form to db

Posted: Mon Jun 29, 2009 5:27 am
by markthien
Hi guys,

I need to let user input chinese/non-ascii character from a textarea. However, after user submit it and save into mysql 5 db, all the characters were deformed .... i mean unknown characters. Can anyone show me step by step how can I fix this problem?

Thanks & Regards,
Mark

Re: saving chinese/non-ascii character from input form to db

Posted: Mon Jun 29, 2009 6:49 am
by jazz090
ok i dealt with this frustration for two weeks and here is what you do:

1. make sure there is a html meta tag specifying a UTF-8 encoding on your submission form
2. turn magic quotes off
3. when you create your mysql table, make sure you set the collation of that field that takes Chinese characters to utf8_unicode_ci
4. escape with mysql_real_escape_string() and upload to db as you would normally
5. once you request that information back (i assume its for displaying), again make sure the page has meta tags specifying UTF-8 and just echo it, it will work
it might looked messed up in mysql but the output is exactly what went in. if you are working with dreamweaver, once you create a new page, it will automatically add the UTF-8 meta tags for you, if not, add this in the <head> tags:

Code: Select all

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />