首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是什么原因导致在MySQL表中插入带有AngularJS和Codeigniter 3的数据?

是什么原因导致在MySQL表中插入带有AngularJS和Codeigniter 3的数据?
EN

Stack Overflow用户
提问于 2019-10-31 21:33:08
回答 1查看 193关注 0票数 1

我正在使用Codeigniter 3.1.8AngularJS v1.7.8开发一个博客应用程序

应用程序的仪表板是“纯”Codeigniter,带有模型、控制器和视图,而前端由AngularJS管理和显示的JSON组成。

我一直无法添加来自前端的评论,通过AngularJS。

我的AngularJS控制器如下所示:

代码语言:javascript
运行
复制
 // Post comment
.controller('PostCommentController', ['$scope', '$http', '$routeParams', 
  function($scope, $http, $routeParams) {
    const slug = $routeParams.slug;
    $http.get('api/' + slug).then(function(response) {

        let post_id = response.data.post.id

        $scope.newComment = {
            slug: $routeParams.slug,
            post_id: post_id,
            name: $scope.name,
            email: $scope.email,
            comment: $scope.comment
        };

        $scope.createComment = function(){
            console.log($scope.newComment);
            $http.post('api/comments/create/' + post_id, $scope.newComment);
        };

    });
}])

我的Codeigniter注释控制器(在API中):

代码语言:javascript
运行
复制
class Comments extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function create($post_slug){
        $data = json_decode(file_get_contents('php://input'), TRUE);
        $this->Comments_model->create_comment($post_id);        
    }

}

Comments_model模型中,我有:

代码语言:javascript
运行
复制
public function create_comment($post_id) {
    $data = [
        'post_id' => $post_id,
        'name' => $this->input->post('name'),
        'email' => $this->input->post('email'),
        'comment' => $this->input->post('comment'),
        'aproved' => 0,
        'created_at' => date('Y-m-d H:i:s')
    ];
    return $this->db->insert('comments', $data);
}

console.log($scope.newComment);函数中的行createComment返回一个包含所有必要的“详细信息”的对象:

代码语言:javascript
运行
复制
{
   comment: "Test Angular",
   email: "razvan_zamfir80@yahoo.com",
   name: "Razvan Zamfir",
   post_id: "67",
   slug: "who-loves-a-butterfly"
}

然而,我得到了这个url:api/comments/create/67api/comments/create/67错误,MySQL插入语句如下所示(我在url /api/MySQL/MySQL/67的浏览器中看到它):

代码语言:javascript
运行
复制
INSERT INTO `comments` (`post_id`, `name`, `email`, `comment`, `aproved`, `created_at`) VALUES (NULL, NULL, NULL, NULL, 0, '2019-10-31 22:06:01')

我的错误在哪里?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-01 12:07:42

您需要修改Comments_model::create_comment方法:

代码语言:javascript
运行
复制
public function create_comment($commentData) {
    $data = [
       'post_id' => $commentData['post_id'],
       'name' => $commentData['name'],
       'email' => $commentData['email'],
       'comment' => $commentData['comment'],
       'aproved' => 0,
       'created_at' => date('Y-m-d H:i:s')
    ];

    return $this->db->insert('comments', $data);
}

并使用$data调用控制器:

代码语言:javascript
运行
复制
public function create($post_slug){
    //also you can use $data = json_decode($this->input->raw_input_stream, TRUE);
    $data = json_decode(file_get_contents('php://input'), TRUE);
    $this->Comments_model->create_comment($data);        
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58651417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档