Update query in codeigniter Example

Today, We want to share with you Update query in codeigniter Example.In this post we will show you update query in ci, hear for update query in codeigniter using multiple where condition we will give you demo and example for implement.In this post, we will learn about update query in codeigniter using where condition with an example.

Update query in codeigniter Example

There are the Following The simple About update table in codeigniter Full Information With Example and source code.

also you can read my prev Post Like as self join, left join, multiple joins, sql join multiple tables, with codeigniter join 2 tables, and join 3 tables in codeigniter as well as Join with multiple tables.

As I will cover this Post with live Working example to develop Database Connection and Queries in Codeigniter, so the codeigniter db query – codeigniter insert id is used for this example is following below.

codeigniter update query

Update query in codeigniter Example
Update query in codeigniter Example

In PHP Based Freamworks Like as CodeIgniter, update() functions is used to change or update existing rows in database table. In order to all the data set of the update sql query, update() functions is used along with simple used of the all tha mysql collumn data set() as well as where() methods in following Below ways

CodeIgniter UPDATE Query-run UPDATE Query-CodeIgniter

CodeIgniter UPDATE Query – how to run UPDATE Query in CodeIgniter

In this post we will show you how to execute CodeIgniter UPDATE Query with update(), update_string() and update_batch().

CodeIgniter ‘UPDATE’ query execute by following functions
  • $this->db->update()
  • $this->db->update_string()
  • $this->db->update_batch()

Codeigniter (CRUD) Select Insert Update Delete Query

  1. Update Query In Codeigniter
  2. Select Query In Codeigniter
  3. Insert Query In Codeigniter
  4. Delete Query In Codeigniter

Update using $this->db->update()

This is how you can update an existing rows into ’member_master’ table using $this->db->update().by using $this->db->update() method we can use UPDATE for single record and extecut CodeIgniter UPDATE Query.

$data = array(
	'member_code' => 'MEM10',
	'member_dept' => 'Devloping'
	);

$this->db->update('member_master',$data,'member_ID=1');

Or as an array:

$this->db->update('member_master',$data,array('member_ID' => 1));

CodeIgniter UPDATE Query with $this->db->update_string()

by using $this->db->update_string() method we can use UPDATE for string type recoder/data and extecut UPDATE Query.

Note: Values are automatically escaped, producing safer queries.

$get_teamname = $_POST['teamname'];

$member_data = array(
    'teamname' => $get_teamname
);
$member_where = "status = 'member'";

//update member data 
$string = $this->db->update_string('tbl_all_member', $member_data, $member_where);

// sql UPDATE query like 
// UPDATE tbl_all_member SET teamname = '$get_teamname' WHERE id = $member_id 

CodeIgniter UPDATE Query with $this->db->update()

by using $this->db->update() method we can use UPDATE for single record and extecut CodeIgniter UPDATE Query.

//get member id
$member_id = $_POST['id'];
//get member name
$get_name = $_POST['name'];
//get member teamname
$get_teamname = $_POST['teamname'];
//get member age
$get_age = $_POST['age'];

$member_data = array(
    'name'      =>  $get_name ,
    'teamname' =>  $get_teamname,
    'age'       =>  $get_age 
);

$this->db->where('id', $member_id);
//update member data 
$this->db->update('tbl_all_member', $member_data);
// sql UPDATE query like 
// UPDATE tbl_all_member SET name = '$get_name', teamname = '$get_teamname', age = '$get_age' WHERE id = $member_id 

Update with $this->db->where()

This is how you can update an existing rows into ’member_master’ table using $this->db->update() along with $this->db->where().

$data = array(
	'member_code' => 'MEM10',
	'member_dept' => 'Devloping'
	);
$this->db->where('member_ID', $id);
$this->db->update('member_master',$data);

Update with $this->db->set()

This is how you can update an existing rows into ’member_master’ table using $this->db->update() along with $this->db->set().

$data = array(
	'member_code' => 'MEM10',
	'member_dept' => 'Devloping'
	);
$this->db->set($data)
$this->db->where('member_ID', $id);
$this->db->update('member_master');

Update using $this->db->update_batch()

This is how you can update an existing rows into ’member_master’ table using $this->db->update_batch().

$data = array(
   array(
      'member_ID' => 1 ,
      'member_code' => 'MEM10' ,
      'member_dept' => 'Devloping'
   ),
   array(
      'member_ID' => 2 ,
      'member_code' => 'MEM20' ,
      'member_dept' => 'Devloping'
   )
);

$this->db->update_batch('member_master', $data, 'member_ID');

// Produces:
// UPDATE `member_master` SET `member_code` = CASE
// WHEN `member_ID` = 1 THEN 'MEM10'
// WHEN `member_ID` = 2 THEN 'MEM20'
// ELSE `member_code` END,
// `member_dept` = CASE
// WHEN `member_ID` = 1 THEN 'Devloping'
// WHEN `member_ID` = 2 THEN 'Devloping'
// ELSE `member_dept` END
// WHERE `member_ID` IN (1,2)

by using $this->db->update_batch() method we can use UPDATE for mualtiple data and same time, with one execution and execute UPDATE Query. by using this method we can execute number of records with single execute query.

// pass member data in array 
$member_data = array(
   array(
    'name'  = >  'add_anme_1' ,
    'teamname'= >  'add_your_team_name1',
    'age'   = >  '24'
   ),
   array(
    'name'  = >  'add_anme_2' ,
    'teamname'= >  'add_your_team_name2',
    'age'   = >  '25'
   )
);
//update member data 
$this->db->update_batch('tbl_all_member', $member_data);

// sql UPDATE mulatiple data in query like  
// UPDATE tbl_all_member SET name = '$get_name', teamname = '$get_teamname', age = '$get_age' WHERE id = $member_id
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about select query in codeigniter with example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment