Sum or subtract a field’s value in a database with Codeigniter

Asked

Viewed 932 times

0

I am using Codeigniter in my application, what I need to do is the following I have in my database a field with name stock, this field is whole value, I need to do the following when I perform a sale it will have to decrease the amount of my sale of that field, in PHP would look something like this.

$sql = mysql_query( sprintf( “UPDATE profile_posts SET cliks = cliks + 1 WHERE id = %d” , $id ) )
    or die( mysql_error( ) );

Is there a Codeigniter feature that I can do this or will have to do a specific SQL for it:

1 answer

1


See an example of DOC

$data = array( 'title' => $title , 'name'  => $name );
$this-> db-> where( 'id' , $id );
$this-> db-> update( 'mytable' , $data );

Your query can be written as follows, using set to assign the count

$this->db-> where( 'id' , $id );
$this->db-> set( array( 'cliks' => 'cliks-1' ) );
$this->db-> update( 'profile_posts' );

I need to do the following when I hold a sale he will have to diminish the amount

Although your query is cliks + 1, I suppose you missed the signal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.