ADD VALUE TO CURRENT COLUMN VALUE IN CODEIGNITOR

There is many times you want to add the values to the current value of column of database table. That is you want to add certain values to the current value of database. Most of the time, we first fetch the current value and then and the value to the fetch value and finally update the database table

Here is the tutorial about how to add certain value to column of the database table directly. In CodeIgnitor we can directly add value to the database column value.
ADD VALUE TO CURRENT COLUMN VALUE IN CODEIGNITOR

1. First create a database

We can create a new database using CREATE DATABASE statement.

CREATE DATABASE company;

2. create a database table 

We can create a new database table using CREATE TABLE statement.

CREATE TABLE IF NOT EXISTS `sales` (
  `sid` int(4) NOT NULL,
  `points` int(4) NOT NULL,
  `section` varchar(200) NOT NULL,
  `address` archar(50) NOT NULL
);

3. Insert data into table

Now insert some data using INSERT INTO operation of MYSQL as follows;

INSERT INTO sales
VALUES (1,30,'sales','lalitpur');

Now we can  add 20 to the current value of points of sales table.

4. Add points to current database value

In CodeIgnitor, this can be done as:

$points=20;
$this->db->set('points', "points+$points",FALSE);
$res=$this->db->update('sales');

The third parameter 'FALSE' is important in above SQL SET statement.It helps to directly extract and change the database value.This SQL statement add 20 to the current value of points of sales table.

similarly, we can subtract, divide and multiply the value with current value of database.

We keep on updating such tutorials. 

For more such tutorials like our Facebook page. 


Comments

Popular posts from this blog

INTEGRATE BOOTSTRAP WITH PHP CODEIGNITER FRAMEWORK

CodeIgnitor installing guide

Selecting data from database in CodeIgnitor