Increment field of mysql database using codeigniter's active record syntax

前端 未结 3 916
粉色の甜心
粉色の甜心 2020-12-08 02:03

I have the following php-codeigniter script which attempts to increment a field of a record using active-record syntax:

$data = array(\'votes\' => \'(vote         


        
相关标签:
3条回答
  • 2020-12-08 02:14

    You can do as given below:

        public function increament_product_count(){
            $product_id=$this->input->post('product_id');
            $this->db->where('id', $product_id);
            $this->db->set('click_count', 'click_count+1', FALSE);
            $this->db->update('tbl_product');
    
        }
    
    0 讨论(0)
  • 2020-12-08 02:25

    You can do as given below:

    $this->db->where('id', $post['identifier']);
    $this->db->set('votes', 'votes+1', FALSE);
    $this->db->update('users');
    

    The reason this works is because the third (optional) FALSE parameter tells CodeIgniter not to protect the generated query with backticks ('). This means that the generated SQL will be:

    UPDATE users SET votes= votes + 1 WHERE id= '44'

    If you notice, the backticks are removed from '(votes+1)', which produces the desired effect of incrementing the votes attribute by 1.

    0 讨论(0)
  • 2020-12-08 02:30
    $data = array('votes' => 'votes + 1');
    
    foreach ($data as $key=>$val) {
        $this->db->set($key, $val, FALSE);
    }
    
    $this->db->where('id', $post['identifier']);
    $this->db->update('users', $data);
    
    0 讨论(0)
提交回复
热议问题