CodeIgniter Basic Tutorial
Performance & Utilities
CodeIgniter Advanced
Updating data is an essential part of any web application.
In CodeIgniter, we can update records using:
Ensure your database is connected:
$autoload['libraries'] = array('database');
Or load manually in your controller:
$this->load->database();
class UpdateController extends CI_Controller { public function update_user($id) { $data = array( 'name' => 'Updated Name', 'email' => 'updated@example.com' ); $this->db->where('id', $id); // WHERE id = ? $this->db->update('users', $data); // UPDATE users SET name=?, email=? WHERE id=? if ($this->db->affected_rows() > 0) { echo "User updated successfully!"; } else { echo "No changes made!"; } } }
👉 URL Example:
http://your-site/index.php/updatecontroller/update_user/1
public function update_status() { $data = array('status' => 1); $this->db->where('status', 0); $this->db->where('email LIKE', '%@example.com'); $this->db->update('users', $data); echo $this->db->affected_rows() . " users activated."; }
CodeIgniter’s Query Builder does not support LIMIT
in updates, but you can use raw SQL:
public function update_limit() { $sql = "UPDATE users SET status = 1 WHERE status = 0 LIMIT 5"; $this->db->query($sql); echo "Limited update applied!"; }
public function update_from_form() { $id = $this->input->post('id'); $name = $this->input->post('name'); $email = $this->input->post('email'); $data = array( 'name' => $name, 'email' => $email ); $this->db->where('id', $id); $this->db->update('users', $data); echo "User updated from form input!"; }
Before update:
ID: 1 | Name: John Doe | Email: john@example.com
After update:
ID: 1 | Name: Updated Name | Email: updated@example.com