Welcome Guest, Not a member yet? Register   Sign In
Problem with 'insert'
#1

Hello,
I have following issue when trying to insert into database. I have already used this approach few times and it always worked for me. Now it doesn't want to work and I have no idea how to solve this. Please help Smile

blad:
Quote:("You must use the "set" method to update an entry.")

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Post_model extends CI_Model {

    public function create( $post )
    {
        $this->db->insert( 'posts' , $post );
    }
}

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Post extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $post = file_get_contents( 'php://input' );
        $_POST = json_decode( $post , true );

        $this->load->model( 'site/Post_model' );    
    }

    public function create()
    {
        $post = $this->input->post('post');
        $this->Post_model->create( $post );
    }

}

/* End of file Post.php */
/* Location: ./application/controllers/Post.php */

Code:
controllersUser.controller('postCreate', ['$scope','$http', function($scope, $http){
    $scope.formSubmit = function(post)
    {
        $http.post('api/site/post/create/',{
            question: post.question,
            answers: post.answer,
            votes: post.votes
        }).success(function(data){
            console.log('post dodany');
        }).error(function(){
            console.log('post NIE dodany');
        });
    };

}]);
Code:
<div>
 <input type="text" ng-model="post.question">
 <input type="text" ng-model="post.answer">
 <input type="number" ng-model="post.votes">
</div>
[Image: 25uqoep.jpg]
Reply
#2

If the $_POST array does not have anything in it, $this->input->post returns NULL, and then when NULL is passed to Active Record's insert method, it gives you that error. So you need to do a little validation before blindly doing a database insert.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB