Ajax PHP插入MySQL utf-8字符不起作用

时间:2014-02-23 07:44:25

标签: php jquery mysql ajax utf-8

我有这个收集数据并发送到php的javascript:

$('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。问题出在哪里?

1 个答案:

答案 0 :(得分:0)

您的HTML页面必须以UTF-8格式发送(因为这是用户输入数据的位置)。您可以通过设置header('content-type: text/html; charset:utf-8');或在head标记中输出相应的META标记来完成此操作。然后,您必须在PHP脚本中调用mysql_set_charset('utf-8')以指定要INSERT的数据是UTF-8。它应该工作。