简体   繁体   English

PHP&MySQL特殊字符编码问题

[英]PHP & MySQL special character encoding problem

When a user submits a special character ♠ 当用户提交一个特殊字符♠ it's stored in MySQL database as â and if a user wants to change it instead of displaying it back as ♠ 它以â 的形式存储在MySQL数据库中,如果用户想更改它而不是将其显示为♠ its displayed as â how can I fix this problem so that its dsiplayed back as ♠ 其显示为â 我怎么能解决这个问题,以便其dsiplayed早在♠ and saved as ♠ 并另存为♠ ?

On a side note how should I save my special characters using PHP? 附带说明一下,如何使用PHP保存特殊字符?

I'm using PHP & MySQL 我正在使用PHP和MySQL

  1. User types in data 用户输入数据
  2. You escape that data to avoid SQL injection (don't convert the special characters to html code equivalent yet) 您转义该数据以避免SQL注入(请不要将特殊字符转换为等效的html代码)
  3. Data gets stored in the database exactly how user typed it in 数据将完全按照用户输入的方式存储在数据库中
  4. You pull the raw data back out 您拉回原始数据
  5. You run the raw data through a character encoding function or something equivalent to convert special characters to their html codes thus avoiding cross site scripting or html injection 您可以通过字符编码功能或等效功能将原始数据运行,以将特殊字符转换为其html代码,从而避免跨站点脚本编写或html注入

That's could be a problem. 那可能是个问题。
If you want to convert your special characters into entities, you have to htmlencode them twice when outputting into field value/textarea content. 如果要将特殊字符转换为实体,则在输出为字段值/文本区域内容时必须对它们进行html编码两次 But it could mess with other characters - all become their entity representations - quotes, brackets and such. 但这可能会与其他字符(全部成为其实体表示形式),引号,方括号等混淆。 If it's what you're asked for - go ahead. 如果这是您的要求-继续。 But, in my opinion, it could be a terrible mess to edit such a text. 但是,在我看来,编辑这样的文本可能是一团糟。
That's why it's better not to let users to use entities. 这就是为什么最好不要让用户使用实体。 Why can't they enter the symbol itself? 他们为什么不能输入符号本身?

As for the special characters in your database - just use UTF-8 encoding in both database and HTML. 至于数据库中的特殊字符-只需在数据库和HTML中都使用UTF-8编码即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM