|
我用的是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复制代码 <?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中正常运行,中文插入和显示正常。 |
-
|