How to update data in codeigniter

Create a view file update.php and save the below code it in application/views/insert.php.

<!DOCTYPE html>
<html>
<head>
<title>update form</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <form action="<?= base_url('demo/update') ?>" method="post">
                    <label>Name</label>
                    <input type="hidden" name="id" class="form-control" value="<?php echo $data->id; ?>">
                    <input type="text" name="name" class="form-control" value="<?php echo $data->name; ?>">
                    <br/>
                    
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" value="<?php echo $data->email; ?>">
                    <br/>
                    
                    <label>Password</label>
                    <input type="text" name="password" class="form-control" value="<?php echo $data->password; ?>">
                    <br/>
                    
                    <input type="submit" value="Update" class="form-control">
                </form>
            </div>
        </div>
    </div>
</body>
</html>

Create a controller file Demo.php and save it in application/controller/Demo.php.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Demo extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model("Demo_Model");
    }
    function insert(){
        $this->load->view("insert");
    }
    
    function save(){
        $data = $this->input->post();
        $result = $this->Demo_Model->save("demo",$data);
        if (!empty($result)){
            redirect(base_url("demo/insert"));
        }
    }
    
    function display(){
        $data["data"] = $this->Demo_Model->get_all("demo");
        $this->load->view("display",$data);
    }
    
    function delete(){
        $id = $this->input->get("id");
        $this->Demo_Model->delete("demo",array("id" => $id));
    }
    
    function update(){
        if($this->input->post("id")){
            $data = $this->input->post();
            $this->Demo_Model->update("demo",$data);
        }else{
            $id = $this->input->get("id");
            $data["data"] = $this->Demo_Model->get_row_by_id("demo",array("id" =>$id));
            $this->load->view("update",$data);
        }
    }
}
   
   

Create a model file Demo.php and save it in application/models/Demo_Model.php.

<?php

class Demo_Model extends CI_Model{
    
    function save($table_name,$data){
        $this->db->insert($table_name,$data);
        return $this->db->insert_id();
    }
    
    function get_all($table_name){
        $this->db->select("*");
        $this->db->from($table_name);
        $query = $this->db->get();
        return $query->result();
    }
    
    function delete($table_name,$where){
        $this->db->where($where);
        $this->db->delete($table_name);
        redirect("demo/display");
    }
    
    function get_row_by_id($table_name,$where_array){
        $this->db->select("*");
        $this->db->from($table_name);
        $query = $this->db->get();
        return $query->row();;
    }
    
    function update($table_name,$data){
        $this->db->update($table_name, $data);
        redirect("demo/display");
    }
}

Let us execute this example by the following URL in the browser. This URL may be different based on your website.

https://howtowebcode.com/demo/display