haoren26 发表于 2011-9-25 21:08:38

插入数据库不稳定

本帖最后由 haoren26 于 2011-9-25 21:19 编辑

CI版本:2.0.3

OS版本:win2003(虚拟机)

看一哈子视频第二部新闻发布系统的时候,在页面上添加评论,后台向数据库插入数据时经常遇到error 1054
代码是照着视频照抄的

Mhome.php函数 insert_comment()
function insert_comment()
      {
                        $content = $this->input->post('comment_content');
                        $article_id = $this->input->post('article_id');
                        $author = $this->input->post('comment_author');
                        $query = $this->db->query("INSERT INTO comments(id,article_id,content,author,last_date) VALUES('',$article_id,$content,$author,now())");
                        return $this->db->affected_rows();
      }

控制器home.php调用 insert_comment方法代码
function comment_ok()
      {
                if ($this->input->post('submit')) {
                        $this->load->model('Mhome');
                        $query = $this->Mhome->insert_comment();
                        if ($query) {
                              redirect('home/content/'.$this->input->post('article_id'));
                        }
                }
      }

但当我想网页表单提交表单时,有时行,有时就会报错(例如输入评论12331,作者sdf)
Error Number: 1054
Unknown column 'sdf' in 'field list'
INSERT INTO comments(id,article_id,content,author,last_date) VALUES('',2,12331,sdf,now())

估计是INSERT语句有问题,但仔细跟视频里比对过,没什么不一样啊。。不得其解,非常郁闷,大家教教我这是怎么回事??
会不会是数据库字段编码的问题??


PS:”有时候行“是指浏览器第一次添加评论,且添加的评论为纯数字


jeongee 发表于 2011-9-25 21:19:40

要加单引号

yuzhigang5460 发表于 2011-9-25 21:23:46


$data = array('article_id'=>$article_id, 'author' => $author, 'content'=>$content,'last_date'=>now());
$query = $this->db->insert('comments', $data);
return $this->db->affected_rows();

好端端的函数不用,非要自己构造insert语句。

haoren26 发表于 2011-9-25 21:41:01

jeongee 发表于 2011-9-25 21:19 static/image/common/back.gif
要加单引号

我用如下方法解决了
$query = $this->db->query("INSERT INTO comments(id,article_id,content,author,last_date) VALUES('','$article_id','$content','$author',now())");
给每个变量都加上单引号
现在可以任意输入数字字母中文了~
你是指这样吗?

Hex 发表于 2011-9-26 00:23:56

haoren26 发表于 2011-9-25 21:41 static/image/common/back.gif
我用如下方法解决了
$query = $this->db->query("INSERT INTO comments(id,article_id,content,author,la ...

参考你的楼上的答案。
这种简单 SQL 不要自己写,直接用 Active Record。
不知道什么是 Active Record 可以搜索论坛。

guanliyang 发表于 2011-9-26 08:07:44

一哈子的视频确实有几个不规范的地方,他做视频的时候也是刚刚接触CI,期望Hex十一有空弄几个牛x的视频教程.......:D

haoren26 发表于 2011-9-26 23:14:36

guanliyang 发表于 2011-9-26 08:07 static/image/common/back.gif
一哈子的视频确实有几个不规范的地方,他做视频的时候也是刚刚接触CI,期望Hex十一有空弄几个牛x的视频教程 ...

有没有比较好的视频推荐一下??谢谢

309090518 发表于 2012-2-1 15:00:28

插入没反应。。。。。
页: [1]
查看完整版本: 插入数据库不稳定