alphaliang 发表于 2011-3-23 16:41:50

CI 2.0.1在Mysql无法插入中文数据

我用的是Apache/2.2.2 (Win32), PHP/5.2.9-1,Mysql 5.0.67,MySQL 字符集: UTF-8 Unicode (utf8),CI 2.0.1!
在代码中用了"set names 'gb2312'",只能从数据库中读取中文数据,显示正常;但是从浏览器上输入的中文却无法输入到数据库中,
下图为出错提示

以下为控制器的代码 blog.php
<?php
class Blog extends CI_Controller{
        function __construct()
        {
                parent::__construct();
                $this->load->database();
                $this->db->query("set names 'gb2312'");
                $this->load->helper('url');
                $this->load->helper('form');
        }

        function index()
        {
                $data['title']="My Blog Title";
                $data['heading']="My Blog Heading";
                $data['query']=$this->db->get('entries');
                $this->load->view('blog_view',$data);
        }
       
        function comments()
        {
                $data['title']="My Comment Title";
                $data['heading']="My Comment Heading";
                $this->db->where('entry_id',$this->uri->segment(3));
                $data['query']=$this->db->get('comments');
                $this->load->view('comment_view',$data);
        }
       
        function comment_insert()
        {       
                $this->db->insert('comments',$_POST);
                redirect('blog/comments/'.$_POST['entry_id']);
        }       
}

还有视图blog_view.php
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<h1><?=$heading?></h1>
<?php foreach ($query->result() as $row):?>
<h3><?=$row->title?></h3>
<p><?=$row->body?></p>
<p><?=anchor('blog/comments/'.$row->id,'Comments');?></p>
<hr>
<?php endforeach;?>
</body>
</html>
视图comment——view.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><?=$title?></title>
</head>
<body>
<h1><?=$heading?></h1>

<?php if($query->num_rows()>0): ?>
<?php foreach ($query->result() as $row):?>
<p><?=$row->body?></p>
<p><?=$row->author?></p>

<hr>
<?php endforeach;?>
<?php endif; ?>
<p><?=anchor('blog','Back to Blog');?></p>

<?=form_open('blog/comment_insert');?>
<?=form_hidden('entry_id',$this->uri->segment(3));?>
<p><textarea name="body" rows="10"></textarea></p>
<p><input type="text" name="author"/></p>
<p><input type="submit" value="Submit Comment"/></p>
</body>
</html>

但是相同的代码却能在CI 1.7.3中正常运行,中文插入和显示正常。

alphaliang 发表于 2011-3-23 16:46:31

我刚刚接触CI,希望各位高手解救小弟

huboo82 发表于 2011-3-23 20:15:02

确保你的文件也使用了gb2312编码。
还有,干吗不在配置数据库的时候把编码就定下来呢。

liaomars 发表于 2011-3-23 23:56:15

要确认。数据库 网页 网页编辑的编码要统一

alphaliang 发表于 2011-3-24 08:36:45

非常感谢,问题解决了!

washburn 发表于 2011-3-30 15:29:31

回复 5# alphaliang


    怎么解决的??

liren 发表于 2011-4-6 21:40:33

怎么解决说说呀!!! :o

jeongee 发表于 2011-4-6 21:45:09

回复 7# liren


   解决的办法就是编码统一呀

liren 发表于 2011-4-6 21:49:55

回复liren


   解决的办法就是编码统一呀
jeongee 发表于 2011-4-6 21:45 http://codeigniter.org.cn/forums/images/common/back.gif

多谢,就是 html编码,数据库编码,php编码统一吧!

ayin 发表于 2011-4-7 10:56:29

楼主问题解决了人就不见人了。。。。
页: [1] 2
查看完整版本: CI 2.0.1在Mysql无法插入中文数据