2012-10-05 52 views
0

我在某个地方的字符集配置有问题 - 并且需要一些帮助。jquery post将错误的字符集添加到数据库中

我正在使用jQuery jeditable通过php更新数据库的值。当我通过此脚本发布øæå时,数据库将被替换为æøå。我也尝试过utf8_encode,但没有运气。定期交与øæå工作正常

$(document).ready(function() { 
    $('.update_pipu_comment').editable('http://lin01.test.no/save.php?update=pipu_comment', { 
     event : 'dblclick', 
     submit : 'ok', 
     indicator : '<img src="image/indicator.gif">', 
     tooltip : 'doubleclick to edit' 
    }); 
}); 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<?php 
//save.php 
require('config.php'); 
$postvalue = $_POST[value]; 
$id = $_POST[id]; 
$update = $_GET[update]; 

if($update == 'pipu_comment') { 
$res = mysql_query("UPDATE pinpuk SET comment = '$postvalue' WHERE id = '$id'"); 
} 
echo $postvalue; 
+0

你的数据库是否设置了字符集UTF8? – gks

+0

这只是像5小时前刚刚问过http://stackoverflow.com/questions/12737875/mysql-outputs-western-encoding-in-utf-8-php-file/ – Mamsaac

+0

像一个训练有素的眼睛 - 可能是,但我阅读了关于您发布Mamsaac链接的问题的答案,并且根据该问题的答案找不到解决方案 – normarth

回答

1

您可以先设置字符集为UTF-8:

$query = "SET NAMES 'UTF8'"; 
mysql_query($query); // this before any insert 

而且你的代码是不是SQL安全:

$postvalue = mysql_real_escape_string($_POST['value']); 
$id = (int)$_POST['id']; 

由于一个侧面说明,你应该尝试移动到MySQLi或PDO。 Here是一个不错的教程,让你开始使用PDO。

+0

感谢您的提示,我一定会阅读您建议的教程。你建议的代码完美工作。 – normarth

相关问题