昨天查了许久  所有页面统一UTF-8  数据库也是UTF-8-unicode-ci  结果还是乱码
然后尝试GB2312 也不行。。
我发代码给大家 看看 新手 什么都不懂。
html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title><body><form action="test.php"  method="post">姓:<input type="text" name="firstname" />
名:<input type="text" name="lastname" />
年龄:<input type="text" name="age"/>
<input type="submit" /></form>
</body>
</html>
PHP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<?php 
header("Content-type: text/html;charset=gb2312");
$con=mysql_connect("localhost","asd","asd123");
if(!$con){
die("connot connect: ".mysql_error());}
mysql_select_db("my_db",$con);
$sql="INSERT INTO person(FirstName,LastName,Age)
VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
mysql_query($sql,$con);
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";mysql_close($con)
?>
</body>
</html>

解决方案 »

  1.   

    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    这里 写的是gb2312? 怎么不写utf-8呀
      

  2.   

    通过将GB换成UTF8 在连接代码下面增加这一句mysql_query("SET NAMES 'UTF8'");成功解决问题 这是怎么回事呢?    可以在数据库直接设置吗?
    mysql_select_db("my_db",$con);
    mysql_query("SET NAMES 'UTF8'");
      

  3.   

     SET NAMES 'UTF8' 这可以支持在数据库 执行
      

  4.   

    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />这句要修改为:<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      

  5.   

    在执行数据库操作之前,先执行:
    mysqli_query("set names utf8", $conn);
      

  6.   

    你能和我讲一下为SET NAMES UTF8是做什么吗?  可以在数据库执行与这一代码等效的操作吗?