我有這個JavaScript用於收集數據併發送到PHP:阿賈克斯PHP inserto到MySQL UTF-8字符不工作
$('a#correct').click(function(event){
trid = $(this).closest("tr").attr('id');
word = $('tr#'+trid+' td#word').html();
translation = $('tr#'+trid+' td#translation input').val();
if(translation.length==0)
{
//validation
alert('Can not be empty');
}
else
{
vid = $('tr#'+trid+' td#vid').html();
to_lang = $('#to_lang').val();
$.ajax({
url: "word_actions.php",
type: "POST",
dataType: "JSON",
data: {
'action': 'correct',
'word':word,
'translation':translation,
'to_lang' : to_lang,
'value_id' : vid,
},
success: function(data){
$('tr#'+trid+' td#catalog').html(data['catalog']);
$('tr#'+trid+' td#word').html(data['word']);
$('tr#'+trid+' td#translation input').val(data['translation']);
}
});
}
});
在服務器端我有這個PHP文件:
$word = $_POST['word'];
$translation = $_POST['translation'];
$to_lang = $_POST['to_lang'];
$value_id = $_POST['value_id'];
if(strlen($word)!=0 && strlen($translation)!=0 &&
strlen($to_lang)!=0 && strlen($value_id)!=0)
{
//insert to monitor
$config = new Config();
$mysql = mysqli_connect($config->m_host, $config->m_user,
$config->m_password, $config->m_database);
if(mysqli_connect_errno())
{
echo 'Failed to connect to MySQL: '.mysqli_connect_error();
}
//select from catalogs_values that not checked by given user
$sql = "INSERT INTO monitor(userid, valueid, name, langid, translate)
VALUES('$user_id', '$value_id', '$word', '$to_lang', '$translation')";
$query = mysqli_query($mysql, $sql) or die(mysqli_error($mysql));
//select 10 identical words and insert it to checked add users table
}
問題是,在MySQL表中,我得到了錯誤的編碼,例如對於我收到的非ASCII字符:Ð「ондураÑ
MySQL的字符集是utf8_general_ci。哪裏可以解決問題?
我該如何檢查? – torayeff
我的意思是如果你從數據庫中讀取問題並且正確地呈現你的HTML,無論他們在數據庫上看起來如何。我也有utf8_general_ci和unicode字符我看到相同。 – 2014-02-23 08:00:00
我想我的問題是在Ajax和PHP之間,因爲當我只是在PHP中回顯結果,它給了我那個錯誤 – torayeff