2017-07-27 43 views
1

我在MySQL数据库有POST_ID,post_number,post_like笨更新特定值

我使用MVC的笨从MySQL表获取所有数据,并在页面上显示它的表(此部分已经完成)

我需要添加一个按钮,每个POST_ID,增加+1至post_like在mysql表,对应于POST_ID。

所以,如果我点击了+1按钮POST_ID 7,post_like应该只增加POST_ID 7

这里是我到目前为止!

view_page.php

 <?php foreach($members as $value): ?> 
     <tr> 
      <td><?php echo $value['post_id']; ?></td> 
      <td><?php echo $value['post_like']; ?></td> 
      <td> <a href="plusone/<?php echo $value['post_id']; ?>">Add +1</a></td> 
     </tr> 
     <?php endforeach; ?> 

controller_page.php

<?php 


    function plusone($post_id){ 

     $this->load->model('model_page'); 
     $this->model_page->data_addition($post_id); 
     redirect('controller_page/viewdata'); 

    } 

    ?> 

Model_page.php

 <?php 

    function data_addition($post_id){ 

     //trying to add 1 the post_like and update it in the db 
     $add_one_to_data = $post_like+ 1; 

     $data = array(
      'post_like' => $add_one_to_data +1 
     ); 

     $this->db->where('post_id', $post_id); 
     $this->db->update('post_tbl', $data); 

    } 

    ?> 

请不要“T嘲笑我的阵列在上面添加的$ add_one_to_data,我不知道自己还能做些什么

在此先感谢

回答

1

,你需要首先得到ifno像:

function data_addition($post_id){ 
     $post_like = $this->db->get_where('post_tbl', array('post_id' => $post_id))->row(); 

     //trying to add 1 the post_like and update it in the db 
     $add_one_to_data = (int) $post_like->post_like + 1; 

     $data = array(
      'post_like' => $add_one_to_data +1 
     ); 

     $this->db->where('post_id', $post_id); 
     $this->db->update('post_tbl', $data); 

    } 
+0

非常感谢您的帮助。我意识到不需要数组中的第二个$ add_one_to_data +1,因为它已经在上面完成:) – mark

1

更换您的data_addition功能

function data_addition($post_id){ 
    $this->db->set('post_like', 'post_like+1', FALSE); 
    $this->db->where('post_id', $post_id); 
    $this->db->update('post_tbl'); 
} 
+0

非常感谢,非常干净 – mark

+0

如果它有帮助,请投票 –

+0

好的和有效的解决方案。我将发布相同的解决方案。 1up从我身边。 –