thinkPHP添加中文到数据库时变成空字符串

Action代码:
<?php
class IndexAction extends Action{

    public function index()
    {    
        $this->display();
    }
    
    public function add()
    {
     $news=new Model('news');
     if ($data=$news->create())
     {
      dump($data);
      if ($news->add())
      {
       echo "添加成功!";
      }
      else
      {
       echo "添加失败!";
      }
     }
     else
     {
      echo "读取信息失败!";
     }
    }
}
?>

HTML代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="__URL__/add" method="post">
  <input type="text" name="name">
  <input type="text" name="contents">
  <input type="submit" value="发    送">
</form>
</body>
</html>

dump的时候中文正常显示,但添加到数据库中就变成了空字符串

作者: shijunxian   发布时间: 2011-08-01

我已经把问题解决了,主要原因是我的数据库编码和页面编码不一致才出来的这个问题
我在配置文件 config 中把数据库编码设为了 gb2312 , 而我的页面编码是 UTF-8 , 所以要把数据库编码改为 utf8 ,代码如下:
' DB_CHARSET '  => ' utf8 ',
这样在添加中文的时候就能正常显示了

作者: shijunxian   发布时间: 2011-08-11