Mysql database increment field using active codeigniter write syntax

I have the following php-codeigniter script that is trying to increase the write field using the active write syntax:

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

As a result, the following SQL is created:

"UPDATE Users SET votes = '(votes + 1)' WHERE ID = '44' "

What does not start, but this SQL does what I am looking for: "UPDATE users SET vote = (votes + 1) WHERE id = '44' " `<- Note the absence of citations around (votes + 1)

Does anyone know how to implement this type of query with the active syntax of codeigniter?

+58
sql mysql activerecord codeigniter
Jun 16 2018-11-11T00:
source share
3 answers

You can do the following:

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

The reason for this is that the third (optional) FALSE parameter tells CodeIgniter not to protect the generated request with inverse outputs ( ' ). This means that the generated SQL will be:

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

If you notice, backlinks are removed from '(votes+1)' , which gives the desired effect of increasing the attribute of votes by 1.

+113
Jun 22 2018-11-22T00:
source share
 $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); 
+4
Jul 19 '13 at 3:56 on
source share

You can do as 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
Jul 24 '19 at 7:36
source share



All Articles