代码之家  ›  专栏  ›  技术社区  ›  mark

Codeigniter更新特定值

  •  1
  • mark  · 技术社区  · 7 年前

    MySql数据库中有一个表 post\u id、post\u编号、post\u like

    我需要为每个按钮添加一个按钮 post_id 在mysql表中,它对应于post_id。

    这是我到目前为止所拥有的!

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

            <?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); 
    
        }
    
       ?>
    

    $add\u one\u to\u数据 我在数组上面加了一个,我不知道还能做什么

    提前感谢

    2 回复  |  直到 7 年前
        1
  •  1
  •   elddenmedio    7 年前

    你需要先获得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); 
    
        }
    
        2
  •  1
  •   ankit suthar    7 年前

    更换您的 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');
    }