【问题标题】:What causes this failure to insert data in a MySQL table with AngularJS and Codeigniter 3?是什么原因导致无法使用 AngularJS 和 Codeigniter 3 在 MySQL 表中插入数据?
【发布时间】:2019-10-31 21:33:08
【问题描述】:

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

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

我无法通过 AngularJS 从前端添加 post cmets

我的 AngularJS 控制器如下所示:

 // 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 内):

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 模型中我有:

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 函数返回一个包含所有必要“详细信息”的对象:

{
   comment: "Test Angular",
   email: "razvan_zamfir80@yahoo.com",
   name: "Razvan Zamfir",
   post_id: "67",
   slug: "who-loves-a-butterfly"
}

然而,我收到了一个Failed to load resource: the server responded with a status of 500 (Internal Server Error) 错误,这个 url:api/comments/create/67 并且 MySQL 插入语句看起来像这样(我在浏览器中看到它的 url /api/cmets/create/67):

INSERT INTO `comments` (`post_id`, `name`, `email`, `comment`, `aproved`, `created_at`) VALUES (NULL, NULL, NULL, NULL, 0, '2019-10-31 22:06:01')

我的错误在哪里?

【问题讨论】:

  • 如果您使用另一个工具(例如 postman)发送该 json 文件,指向同一个端点,您是否能够插入数据或出现相同的错误?

标签: php angularjs codeigniter-3


【解决方案1】:

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

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 调用它:

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);        
}

【讨论】:

  • 很好的答案 Doru,它奏效了。你能简要解释一下你的代码吗?我想学习。谢谢!
  • $this->input->post() 通常只能在提交内容类型为 multipart/form-data 或 application/x-www-form-urlencoded 的数据时使用(经典的html表单) 但现在您的数据由 Angular 以内容类型 application/json 提交。这就是为什么您需要从 php://input 流中获取数据并将其传递给您的模型的原因。更多信息你可以在这里找到codeigniter.com/user_guide/libraries/…stackoverflow.com/questions/8596216/…
猜你喜欢
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多