【问题标题】:Jquery Ajax, mySQL UTF-8Jquery Ajax,mySQL UTF-8
【发布时间】:2013-11-25 16:48:09
【问题描述】:

页面上有一个表单,标题包含:

<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 字符 ????

谁能帮忙解决这个问题?

【问题讨论】:

  • 在你的数据库中尝试 ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

标签: php jquery mysql ajax utf-8


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 2013-10-31
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多