我已经在<?php那里或者sql语句之前设置utf8,可是插入的数据还是乱码,求大神解答啊
代码如下
<?php  
error_reporting(0);
header("Content-Type:text/html;charset=utf-8");
    if(isset($_POST["Submit"]) && $_POST["Submit"] == "提    交")  
    {  
        
           
                mysql_connect("localhost","root","");   //连接数据库  
                mysql_select_db("xt");  //选择数据库  
                mysql_query("set names 'gdk'"); //设定字符集 
                mysql_query("SET NAMES 'utf8'",$db); 
                $sql = "select godid from sp  where godid = '$_POST[godid]'";//SQL语句  
                $result = mysql_query($sql);    //执行SQL语句  
                $num = mysql_num_rows($result); //统计执行结果影响的行数  
                if($num)    //如果已经发布
                {  
                    echo "<script>alert('商品已发布'); history.go(-1);</script>";  
                }  
                else    //为发布的商品  
                {  
                    $sql_insert = "insert into sp (godid,godname,godtypic,godprice,godjianjie,count,sold) values('$_POST[godid]','$_POST[godname]','$_POST[godtypic]','$_POST[godprice]','$_POST[godjianjie]','','')"; 
                    $res_insert = mysql_query($sql_insert);  
                    //$num_insert = mysql_num_rows($res_insert);  
                    if($res_insert)  
                    {  
                        echo "<script>alert('发布成功!'); history.go(-1);</script>";  
                    }  
                    else  
                    {  
                        echo "<script>alert('发布失败!'); history.go(-1);</script>";  
                    }  
                }   
 
    }  
    
?> <form action=" " method="post"> 
  <div id="neirong">
  <p class="minititle"><strong>发布商品</strong></p>  <p><span>类&nbsp;别:</span><input name="godtypic" type="text" style="width:60px; height:22px"></p>
  <p><span>商品名:</span><input name="godname" type="text" style="width:100px; height:22px">
  <span>编&nbsp;号:</span><input name="godid" type="text" style="width:60px; height:22px">
  <span>价&nbsp;格:</span><input name="godprice" type="text" style="width:80px; height:22px"></p>
  <p>简&nbsp;介:</p>
   <p> <textarea name="godjianjie" style="width:350px; height:66px"></textarea>
  </p>
 <p><input type="Submit"  name="Submit" value="提    交" class="submit-btn"/></p></div>
</form>

解决方案 »

  1.   

    你有  mysql_query("set names 'gdk'"); //设定字符集
    分明是设置成 gdk 这个不存在的字符集,MySQL 是不认账的虽然接下来有 mysql_query("SET NAMES 'utf8'",$db); 
    但数据库连接资源 $db 并不存在,自然也是无效的了
      

  2.   

    设置下数据库编码吧
    mysql> SHOW VARIABLES LIKE 'collation_%';
      

  3.   

    从哪里设置?是在PHP代码里面还是MySQL数据库的配置文件里面?
      

  4.   

    mysql_connect("localhost","root","");   //连接数据库  
    mysql_select_db("xt");  //选择数据库  
    mysql_query("set names utf8"); //设定字符集 只要这三句
    另外请确保你的程序是按 utf-8 字符集保存的
      

  5.   

    参考:http://blog.csdn.net/fdipzone/article/details/18180325