2013-11-25 66 views
0

一個有窗體頁上,標題中包含:jQuery的阿賈克斯,MySQL的UTF-8

<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 

然後,我有一個腳本張貼AJAX數據:

function submitmessage(id){ 
$.ajax({ type :"post", 
    data : { newmessage: $('#message_'+id).val(), 
    newfrom: $('#from_'+id).val(), 
    newto: $('#to_'+id).val(), 
    newadid: $('#adid_'+id).val() }, 
    url : "newcomment.php", 
    success: function(){ 
     ad_sendmessage(id); 
     $('#message_'+id).val(""); 
    } 
    });}; 

的PHP文件就像這個:

<?php 
if($_POST) { 
    $message=$_POST['newmessage']; 
    $from=$_POST['newfrom']; 
    $to=$_POST['newto']; 
    $adid=$_POST['newadid']; 
    $con=mysqli_connect('localhost','root', '13Sept1983','b2b_test'); 
    // Check connection 
    if (mysqli_connect_errno()) 
    { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    mysqli_query($con,"set names 'utf8'"); 
    $sql="INSERT INTO Message (Message, MesFrom, MesTo, Adid) 
    VALUES ('$message', $from, $to, $adid)"; 
    if (!mysqli_query($con,$sql)) 
    { 
     die('Error: ' . mysqli_error($con)); 
    } 
    echo "1 record added"; 
    mysqli_close($con); 
}else { } 
?> 

但我仍然在數據庫中獲得UTF-8字符爲????

有人可以幫助解決這個問題嗎?

+1

嘗試ALTER TABLE表名轉換爲字符集是utf8 COLLATE utf8_general_ci在你的數據庫 –

回答

0

試試這個:

mysql_query("ALTER TABLE Message CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci")