How to Edit and Update data in CodeIgniter

How to Edit and Update data in CodeIgniter

In this post i will explain how to edit and update data in codeigniter, after inserted data and retrieved data or records from database then next step to update data or records in database.

Some application, it is important after inserted and retrieve data or records using codeIgniter framework so I will show you how to update data or records in database using codeIgniter framework.

If you are familiar with  insert and retrieve data from database using codeIgniter otherwsie see my link how to insert data in database using codeIgniter and how to retrieve data from database using codeIgniter

Follow these steps

1.   First see how to insert data in CodeIgniter

2.   Next see how to retrieve data in codeIgniter after that

3.   Add a function in model, go to where CodeIgniter is installed path like C:\xampp\htdocs\CITest\application\models and open  employee_model.php and create a function whose name is getEmployee where employee_model is the model name and getEmployee is function inside that model.

          public function getEmployee($emp_id)

         {
             $this->db->where('emp_id', $emp_id);
             $query = $this->db->get('citest_employee');
             return $query->row();
         }

4.   Add a function in controller, go to where codeigniter is installed path like C:\xampp\htdoc\CITest\application\controllers and open employee.php and create function whose name is edit_employee.

         public function edit_employee($emp_id)

         {
             $row = $this->employee_model->getEmployee($emp_id);
             $data['employee'] = $row;
             $this->load->view('edit_employee_form', $data);
         }

5.   Create view go to where codeIgniter is installed path like
C:\xampp\htdocs\CITest\application\views and create view name is edit_employee_form code shown below.

         <!DOCTYPE html>
         <html>
         <head>
           <title>Update Employee Records in Database</title>
         </head>
         <body>
           <form action="<?php echo site_url('employee/update_employee'); ?>" method="post">
           <table align="center">
              <tr>
               <td>Name:</td>
               <td><?php echo form_input(array('id'=>'emp_name', 'name'=>'emp_name', 'placeholder' => 'Name', 'size'=>25, 'value'=> set_value('emp_name', $employee->emp_name)));?></td>
             </tr>
             <tr>
              <td>Gender:</td>
              <td><?php echo form_input(array('id'=>'emp_gender', 'name'=>'emp_gender', 'placeholder' => 'Gender', 'size'=>25, 'value'=> set_value('emp_gender', $employee->emp_gender)));?></td>
             </tr>
             <tr>
              <td>Email:</td>
              <td><?php echo form_input(array('id'=>'emp_email', 'name'=>'emp_email', 'placeholder' => 'Email', 'size'=>25, 'value'=> set_value('emp_email', $employee->emp_email)));?></td>
             </tr>
             <tr>
              <td>Phone:</td>
              <td><?php echo form_input(array('id'=>'emp_phone', 'name'=>'emp_phone', 'placeholder' => 'Phone', 'size'=>25, 'value'=> set_value('emp_phone', $employee->emp_phone)));?></td>
             </tr>
             <tr>
              <td>Address:</td>
              <td><?php echo form_input(array('type'=>'textarea','id'=>'emp_address', 'name'=>'emp_address', 'placeholder' => 'Address', 'size'=>25, 'value'=> set_value('emp_address', $employee->emp_address)));?></td>
             </tr>
             <tr>
              <td></td>
              <td><input type="submit" value="Save" name="submit"></td>
            </tr>      
         </table>
       </form>
     </body>
  </html>

6.   Now open employee_form.php (Reference how to insert data in codeigniter)and add some piece of code at head section

          <th>Action</th>

and body section

          <td><a href='".site_url('employee/edit_employee/'.$row->emp_id)."'>Edit</a></td>

view look like shown below.

         <!DOCTYPE html>
         <html>
         <head>
            <title>Insert Employee Records in Database</title>
         </head>
         <body>
           <form action="<?php echo site_url('employee/employee_form'); ?>" method="post">
           <table align="center">
              <tr>
               <td>Name:</td>
               <td><?php echo form_input(array('id'=>'emp_name', 'name'=>'emp_name', 'placeholder' => 'Name', 'size'=>25));?></td>
             </tr>
             <tr>
               <td>Gender:</td>
               <td><?php echo form_input(array('id'=>'emp_gender', 'name'=>'emp_gender', 'placeholder' => 'Gender', 'size'=>25));?></td>
            </tr>
            <tr>
             <td>Email:</td>
             <td><?php echo form_input(array('id'=>'emp_email', 'name'=>'emp_email', 'placeholder' => 'Email', 'size'=>25));?></td>
            </tr>
            <tr>
             <td>Phone:</td>
            <td><?php echo form_input(array('id'=>'emp_phone', 'name'=>'emp_phone', 'placeholder' => 'Phone', 'size'=>25));?></td>
            </tr>
            <tr>
             <td>Address:</td>
             <td><?php echo form_input(array('type'=>'textarea','id'=>'emp_address', 'name'=>'emp_address', 'placeholder' => 'Address', 'size'=>25));?></td>
            </tr>
            <tr>
             <td></td>
             <td><button type="submit" id="employee-submit">ADD</button</td>
            </tr>      
         </table>
      </form>
      <table border="1" align="center" style="margin-top: 35px; padding:10px;">
        <thead>
          <tr>
            <th>Emp Id</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Address</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          <?php
            foreach ($this->employee_model->getEmployees() as $row)
            {
              echo "<tr>
                          <td>$row->emp_id</td>
                          <td>$row->emp_name</td>
                          <td>$row->emp_gender</td>
                          <td>$row->emp_email</td>
                          <td>$row->emp_phone</td>
                          <td>$row->emp_address</td>
                         <td><a href='".site_url('employee/edit_employee/'.$row->emp_id)."'>Edit</</td>
                      </tr>"; 
            }
          ?>
        </tbody>
      </table>
   </body>
</html>

7.   Go to browser and type localhost/CITest/employee screen shown below

Edit and Update data in CodeIgniter

8.  Again Add a function in controllergo to where CodeIgniter is installed path like C:\xampp\htdocs\CITest\application\controllers and open employee.php and create function whose name is update_employee.


         public function update_employee($emp_id)

         {
             $emp_id = $this->input->post('emp_id');
                  $data = array(

                              'emp_name'      => $this->input->post('emp_name'),
                              'emp_gender'    => $this->input->post('emp_gender'),
                              'emp_email'      => $this->input->post('emp_email'),
                              'emp_phone'     => $this->input->post('emp_phone'),   
                              'emp_address'   => $this->input->post('emp_address')  
                  
                  );
                  $this->db->where('emp_id', $emp_id);
                  $this->db->update('citest_employee', $data);
                  redirect('employee/index');
         }


9.   Now go to view edit_employee_form.php and add some code after table tag, code shown below.

<input type="hidden" name="emp_id" value="<?php echo $employee->emp_id; ?>"> 

view look like shown below

         <!DOCTYPE html>
         <html>
         <head>
           <title>Update Employee Records in Database</title>
         </head>
         <body>
       <form action="<?php echo site_url('employee/update_employee'); ?>" method="post">
           <table align="center">
        <input type="hidden" name="emp_id" value="<?php echo $employee->emp_id; ?>">
        <tr>
          <td>Name:</td>
          <td><?php echo form_input(array('id'=>'emp_name', 'name'=>'emp_name', 'placeholder' => 'Name', 'size'=>25, 'value'=> set_value('emp_name', $employee->emp_name)));?></td>
        </tr>
        <tr>
          <td>Gender:</td>
          <td><?php echo form_input(array('id'=>'emp_gender', 'name'=>'emp_gender', 'placeholder' => 'Gender', 'size'=>25, 'value'=> set_value('emp_gender', $employee->emp_gender)));?></td>
        </tr>
        <tr>
          <td>Email:</td>
          <td><?php echo form_input(array('id'=>'emp_email', 'name'=>'emp_email', 'placeholder' => 'Email', 'size'=>25, 'value'=> set_value('emp_email', $employee->emp_email)));?></td>
        </tr>
        <tr>
          <td>Phone:</td>
          <td><?php echo form_input(array('id'=>'emp_phone', 'name'=>'emp_phone', 'placeholder' => 'Phone', 'size'=>25, 'value'=> set_value('emp_phone', $employee->emp_phone)));?></td>
        </tr>
        <tr>
          <td>Address:</td>
          <td><?php echo form_input(array('type'=>'textarea','id'=>'emp_address', 'name'=>'emp_address', 'placeholder' => 'Address', 'size'=>25, 'value'=> set_value('emp_address', $employee->emp_address)));?></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" value="Save" name="submit"></td>
        </tr>      
      </table>
    </form>
  </body>
</html>


10.   Go to browser and type localhost/CITest/employee after that click edit link screen shown below


Edit and Update data in CodeIgniter

and changes values then click save button after that easily you can see data are changed also you can verify from database, screen shown below.


Edit and Update data in CodeIgniter




                             I hope, it helped you to understand, how to edit and update data in database using CodeIgniter Framework......Thanks

About MSK TUTORIAL

Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. http://www.msktutorial.com/ was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment

3 comments:

yana said...

hai.. Why "insert data" cannot open??

Shahzaib Ahsan said...

Testing your Code Let see how it works

AzkaWEB said...

Great Sharing

Post a Comment

Thanks For Visiting Blog...