我有以下数据要使用 php 保存在数据库中
我的数据是:12 ÷ 5
我有以下代码:
$.ajax({
type: "POST",
url: "add_proc.php",
data: "topic="+ encodeURIComponent(field1)
当我在 firebug 中检查 field1 的值时,它是相同的 12 ÷ 5
,但当值保存在数据库中时,它是 12 ÷ 5
。
$topic=mysql_escape_string($_POST['topic']);
echo($topic);
即使我在发布后回显变量的值,我也会得到12 × 5
。
我不知道问题出在哪里?
最佳答案
÷
采用 UTF-8 编码 -- http://www.fileformat.info/info/unicode/char/00f7/
#pseudo
utf8('÷') => 'Ã' + '·'
utf8(0x00F7) => 0xC3, 0xB7
您可以使用utf8_decode
(我猜编码来自 $_POST
):
$topic = mysql_escape_string(utf8_decode($_POST['topic']));
确保您的数据库/表有 Unicode/UTF-8 support .
或者,将其保留为编码状态,并像页面中的 UTF-8 编码一样echo
(如 Ates Goral 建议的那样),以便浏览器将为您处理解码。
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
关于php - 无法保存 12 ÷ 5 等数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1650236/