Everytime I insert a new data it will say that post_id
cannot be NULL
but when I check phpmyadmin. The correct id is stored.
I'm using ajax to send the id's
from the view to the controller. I've tried alert(id)
to see if the id
is really selected and it is selected so no problem in the jquery part.
It works the way I want it to, it's just that there is this error which does not really stop the insertion and I can hide the error but I would like to fix it without using that.
jquery
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+"
This 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
});
In the alert, I can see the id and the comments and they are correct. So I'm pretty sure there is no problem in accessing.
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');
}
the error
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
Line number 28 in the model is just
public function addcomment($data){
$this->db->insert('post_comments', $data);
}
try changing it to :
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+"
This 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
});
in phpmyadmin try setting the Default value for post_id