<html><head>
book entry result</head><body>
<h1>book entry result</h1>
<?php
header("content-type:text/html;charset=utf-8");
$isbn=$_POST['isbn'];
$author=$_POST['author'];
$title=$_POST['title'];
$price=$_POST['price'];
if(!($isbn||$author||$title||$price))
{
echo"请确认每行都输入数据";
exit;
}
if(!get_magic_quotes_gpc())
{
$isbn=addslashes($isbn);
$author=addslashes($author);
$title=addslashes($title);
$price=addslashes($price);
}
mysql_query('set names utf8');
@$db=new mysqli('localhost','root','root','books');
if(mysqli_connect_error())
{
echo "链接数据库不成功";
exit;
}
header("content-type:text/html;charset=utf-8");
$query="insert into books(isbn ,author ,title ,price)
values('".$isbn."', '".$author."', '".$title."', '".$price."')";
$result=$db->query($query);
if($result){
echo $db->affected_rows." 本书数据录入成功。";
}else{
echo"数据录入失败。";
}?>
</body>
</html>
我已经用header()把php页变成utf-8
数据库和表的的编码都是utf-8

解决方案 »

  1.   

    mysql_query('set names utf8'); 和mysqli不搭调,应该是 $db->query('set names utf8');
    你是在哪儿看到的插入的数据乱码的?phpmyadmin? 
      

  2.   

    是的 我在phpmyadmin里有乱码 试了你的方法 还是乱码
      

  3.   

    我以前遇到过程序编码正常,页面显示正常,唯独phpmyadmin中乱码的情况。那是phpmyadmin的配置没弄好,不过我也不知道怎么改。你可以写个简单的小程序把数据库内容读到页面显示看看
      

  4.   

    从数据库查询显示到浏览器就没乱码 php向数据库插入数据 在phpmyadmin看就是乱码 用cmd查看也是乱码
      

  5.   

    应该先执行mysql_connnet(……),链接好数据库在执行mysql_query("set names utf-8");吧