php - Codeigniter 奇怪的数据库错误

标签 php jquery mysql codeigniter

每次我插入新数据时,它都会说 post_id 不能是 NULL 但是当我检查 phpmyadmin 时。存储了正确的 ID。

我正在使用 ajax 将 id's 从 View 发送到 Controller 。我试过 alert(id) 来查看 id 是否真的被选中并且它被选中所以在 jquery 部分没有问题。

它按照我想要的方式工作,只是有这个错误并没有真正停止插入,我可以隐藏错误但我想在不使用它的情况下修复它。

j查询

var post_id = $(this).closest('.portlet').find('.form').find('.postid').val();
        var comment = $(this).closest('.comments').find('textarea').val();
        alert("This is the post id "+post_id+"\nThis is the comment "+comment);
        $(this).closest('.comments').submit();
        $.ajax({
            type: "POST",
            url: BASE_URL+'classes/addcomment',
            dataType: 'json',
            data: {post_id: post_id, comment: comment},
            async: false
    });

在警报中,我可以看到 id 和评论,它们是正确的。所以我很确定访问没有问题。

Controller

public function addcomment(){
        $data = array(
            'user_id' => $this->user_id,
            'post_id' => $this->input->post('post_id'),
            'content' => $this->input->post('comment')
        );
        $this->Comment_model->addcomment($data);
        redirect('/classes/ICT141');
    }

错误

Error Number: 1048
Column 'post_id' cannot be null
INSERT INTO `post_comments` (`user_id`, `post_id`, `content`) VALUES ('4', NULL, NULL)
Line Number: 28

模型中的第 28 行就是

public function addcomment($data){ 
    $this->db->insert('post_comments', $data);
   }

最佳答案

尝试将其更改为:

var post_id = $(this).closest('.portlet').find('.form').find('.postid').val();
        var comment = $(this).closest('.comments').find('textarea').val();
        alert("This is the post id "+post_id+"\nThis is the comment "+comment);
        $(this).closest('.comments').submit();
        $.ajax({
            type: "POST",
            url: BASE_URL+'classes/addcomment',
            dataType: 'json',
            data: {"post_id" : post_id, "comment" : comment},
            async: false
    });

关于php - Codeigniter 奇怪的数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32694857/

相关文章:

javascript - 在 JavaScript 或 jQuery 中获取部分 View 中的 HTML 类名称

python - 如何在Windows 10上连接web2py和mysql

php - 从表单操作PHP和/或MySql创建多个选择?

javascript - 正确使用History.js和AJAX动态加载

php fgetcsv - 字符集编码问题

mysql - 我可以安全地从 mysql 数据库中删除 .BAK 文件吗?

MySQL如何使用触发器将值减1

php - php/sql 登录脚本故障排除

javascript - 获取相应 tr 中的当前索引

javascript - 如果鼠标不动,如何使叠加层淡出?