CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

Update record CodeIgniter framework PHP


In this example we will discuss about how to update a record or data from MySQL database using CodeIgniter framework PHP.

To update the data in mysql table UPDATE statement is used.

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 

Note: The WHERE clause specifies which data that should be updated. If you omit the WHERE clause, all records or data will be updated!


We use 4 file for update students data.

  1. student_view.php (CodeIgniter\application\controllers\Crud.php )
  2. student_fetch.php (CodeIgniter\application\models\Crud_model.php)
  3. view_data.php (CodeIgniter\application\views\display_records.php)
  4. student_update.php (CodeIgniter\application\views\update_records.php)

Sql Table

 CREATE TABLE crud (
  `id` int(11) NOT NULL,
  `first_name` varchar(30) NOT NULL,
  `last_name` varchar(30) NOT NULL,
  `email` varchar(30) NOT NULL,
   PRIMARY KEY (id)
);

Crud.php (Controller file for Update)

<?php  
class Crud extends CI_Controller 
{
	public function __construct()
	{
	/*call CodeIgniter's default Constructor*/
	parent::__construct();
	
	/*load database libray manually*/
	$this->load->database();
	
	/*load Model*/
	$this->load->model('Crud_model');
	}
	public function dispdata()
	{
	$result['data']=$this->Hello_model->display_records();
	$this->load->view('display_records',$result);
	}
	public function updatedata()
	{
	$id=$this->input->get('id');
	$result['data']=$this->Crud_model->displayrecordsById($id);
	$this->load->view('update_records',$result);
	
		if($this->input->post('update'))
		{
		$first_name=$this->input->post('first_name');
		$last_name=$this->input->post('last_name');
		$email=$this->input->post('email');
		$this->Crud_model->update_records($first_name,$last_name,$email,$id);
		echo "Date updated successfully !”;
		}
	}
	
}
?>

Crud_model.php

<?php
class Crud_model extends CI_Model 
{
    /*Display*/
	function display_records()
	{
	$query=$this->db->query("select * from crud");
	return $query->result();
	}
        function displayrecordsById($id)
	{
	$query=$this->db->query("select * from form where id='".$id.”’”);
	return $query->result();
	}
	/*Update*/
	function update_records($first_name,$last_name,$email,$id)
	{
	$query=$this->db->query("update form SET first_name='$first_name',last_name='$last_name',email='$email' where id='$id’”);
	}
	
}

display_records.php (View file for fetch data)

<!DOCTYPE html>
<html>
<head>
<title>Delete records</title>
</head>
 
<body>
<table width="600" border="1" cellspacing="5" cellpadding="5">
  <tr style="background:#CCC">
    <th>Sr No</th>
    <th>First_name</th>
    <th>Last_name</th>
    <th>Email Id</th>
   <th>Update</th>
  </tr>
  <?php
  $i=1;
  foreach($data as $row)
  {
  echo "<tr>";
  echo "<td>".$i."</td>";
  echo "<td>".$row->first_name."</td>";
  echo "<td>".$row->last_name."</td>";
  echo "<td>".$row->email."</td>";
  echo "<td><a href=‘updatedata?id=".$row->id."'>Update</a></td>";
  echo "</tr>";
  $i++;
  }
   ?>
</table>
 
</body>
</html>

update_records.php (View file for update)

<!DOCTYPE html>
<html>
<head>
<title>Update Data</title>
</head>
 
<body>
 <?php
  $i=1;
  foreach($data as $row)
  {
  ?>
	<form method="post">
		<table width="600" border="1" cellspacing="5" cellpadding="5">
  <tr>
    <td width="230">Enter Your Name </td>
    <td width="329"><input type="text" name="first_name" value="<?php echo $row->first_name; ?>"/></td>
  </tr>
  <tr>
    <td>Enter Your Email </td>
    <td><input type="text" name="last_name" value="<?php echo $row->last_name; ?>"/></td>
  </tr>
  <tr>
    <td>Enter Your Mobile </td>
    <td><input type="text" name="email" value="<?php echo $row->email; ?>"/></td>
  </tr>
  <tr>
    <td colspan="2" align="center">
	<input type="submit" name="update" value="Update_Records"/></td>
  </tr>
</table>
	</form>
	<?php } ?>
</body>
</html>

Now run the program on your browser with the below URL:

http://localhost/codeIgniter/index.php/Student_view

After fetch data the table look like this.

Id first name last name Email Id Action
1 Divyasundar Sahu divyasundar@gmail.com Update
2 Hritika Sahu hritika@gmail.com Update
3 Milan Jena milanjena@gmail.com Update

Now i am going to update the id 3 data. After change the data of id 3 the table look like this.

Id first name last name Email Id Action
1 Divyasundar Sahu divyasundar@gmail.com Update
2 Hritika Sahu hritika@gmail.com Update
3 Milan Kumar Jena milanjena@gmail.com Update