2016-07-23 78 views
0

我正在發送一個ajax請求,它也返回成功消息,但數據未插入。基本上它是一個鏈接,當我將點擊鏈接,然後ajax將向控制器發送一個請求,並在數據庫中增加前值爲1的值。我試過通過巨大的時間,但失敗了。這是一個商業項目。它會很感激,如果你有幫助。Ajax請求返回成功消息,但數據不會插入數據庫

Ajax File : 
 

 
$(document).ready(function(){ 
 

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

 
\t    if (res.success) { 
 
\t    \t alert(res.msg); 
 
       } else { 
 
        alert(res.msg); 
 
       } 
 
\t   } 
 

 
\t  }); 
 

 
\t  return false; 
 
\t }); \t 
 

 
});
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>
控制器的文件:

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


} 

回答

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

我在鏈接後添加'post_id'。這很好。我沒有定義表格行將受到影響的確切ID。

0

更新

if ($this->db->_error_message()) { 
return FALSE; // Or do whatever you gotta do here to raise an error 
} else { 
return $this->db->affected_rows(); 
}` 
+0

我想增加'post_like'行的值。它會計數一個帖子的樣子。 –

0

你是不是調用該函數,你只加載檢查F unction,並且由於文件加載成功,它返回true。 定義函數後,嘗試調用它,看看是否有效。