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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markthien
Forum Commoner
Posts: 33
Joined: Fri Feb 13, 2009 7:50 pm

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

Post 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
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

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

Post 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" />
Post Reply