Shamim Forhad Shamim Forhad - 3 months ago 14x
PHP Question

Ajax request return success message but data does not insert into database

I am sending a ajax request it also returning success message but data is not inserting. Basically it is a link, when I will will click on the link then ajax will send a request to controllers and in database it increase the value with the previous value by 1. I have tried pass huge times but failed. This is a codignator project. It will grateful if you kindly help.

Ajax File :


e.preventDefault(); // <------this will restrict the page refresh
var post_id = $(this).prop('rel');
url: "http://localhost/ci_website/index.php/site/add_like",
type: 'POST',
data : post_id,
dataType: 'json',
success: function(res) {

if (res.success) {
} else {


return false;


View File :

<a id="like" class="like_btn" rel="<?php echo $blog['blog_id'];?>" href="<?php echo site_url('site/add_like') . '/' . $blog['blog_id'];?>">Like</a>

Controller File :

public function add_like()
header('Content-Type: application/json');
if ($this->input->is_ajax_request()) {
$post_id = $this->uri->segment(3);

$this->db->set('post_like', '`post_like` + 1', FALSE);
$this->db->where('blog_id', $post_id);
$add_post_view = $this->db->update('wb_blog');
if ($add_post_view) {
die(json_encode(array('success' => true, 'msg' => 'Your Like has been sent successfully.')));
} else die(json_encode(array('success' => false, 'msg' => 'Something bad happened!!! Please, try again.')));


 url: "http://localhost/ci_website/index.php/site/add_like/"+post_id,

I have add 'post_id' after the link. This is work fine. I did not define the exact id against which the row of the table will be affected.