用户
 找回密码
 入住 CI 中国社区
搜索
查看: 6483|回复: 10
收起左侧

[已解决] CI 2.0.1在Mysql无法插入中文数据

[复制链接]
发表于 2011-3-23 16:41:50 | 显示全部楼层 |阅读模式
我用的是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'",只能从数据库中读取中文数据,显示正常;但是从浏览器上输入的中文却无法输入到数据库中,
下图为出错提示
a.png
以下为控制器的代码 blog.php
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
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
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中正常运行,中文插入和显示正常。
a.png
 楼主| 发表于 2011-3-23 16:46:31 | 显示全部楼层
我刚刚接触CI,希望各位高手解救小弟
发表于 2011-3-23 20:15:02 | 显示全部楼层
确保你的文件也使用了gb2312编码。
还有,干吗不在配置数据库的时候把编码就定下来呢。
发表于 2011-3-23 23:56:15 | 显示全部楼层
要确认。数据库 网页 网页编辑的编码要统一
 楼主| 发表于 2011-3-24 08:36:45 | 显示全部楼层
非常感谢,问题解决了!
发表于 2011-3-30 15:29:31 | 显示全部楼层
回复 5# alphaliang


    怎么解决的??
发表于 2011-4-6 21:40:33 | 显示全部楼层
怎么解决说说呀!!!
发表于 2011-4-6 21:45:09 | 显示全部楼层
回复 7# liren


   解决的办法就是编码统一呀
发表于 2011-4-6 21:49:55 | 显示全部楼层
回复  liren


   解决的办法就是编码统一呀
jeongee 发表于 2011-4-6 21:45


多谢,就是 html编码,数据库编码,php编码统一吧!
发表于 2011-4-7 10:56:29 | 显示全部楼层
楼主问题解决了人就不见人了。。。。

本版积分规则