Home >>Codeigniter Tutorial >Codeigniter update database record

Codeigniter update database record

Codeigniter update database record

In this tutorial, we will understand how to Update records. We will use users table to update records.

Controller

Copy the below given code in your controllers.
<?php
class Hello extends CI_Controller 
{
	public function __construct()
	{
	parent::__construct();
	$this->load->database();
	$this->load->helper('url');
	$this->load->model('Hello_Model');
	}

	public function savedata()
	{
		$this->load->view('registration');
		if($this->input->post('save'))
		{
		$n=$this->input->post('name');
		$e=$this->input->post('email');
		$m=$this->input->post('mobile');
		$this->Hello_Model->saverecords($n,$e,$m);		
		redirect("Hello/dispdata");  
		}
	}
	
	public function dispdata()
	{
	$result['data']=$this->Hello_Model->displayrecords();
	$this->load->view('display_records',$result);
	}
	
	public function deletedata()
	{
	$id=$this->input->get('id');
	$this->Hello_Model->deleterecords($id);
	redirect("Hello/dispdata");
	}
	
	public function updatedata()
	{
	$id=$this->input->get('id');
	$result['data']=$this->Hello_Model->displayrecordsById($id);
	$this->load->view('update_records',$result);	
	
		if($this->input->post('update'))
		{
		$n=$this->input->post('name');
		$e=$this->input->post('email');
		$m=$this->input->post('mobile');
		$this->Hello_Model->updaterecords($n,$e,$m,$id);
		redirect("Hello/dispdata");
		}
	}
}
?>

Display Records View

Copy the below given code in your display_records.php View Page.
<!DOCTYPE html>
<html>
<head>
<title>Display Records</title>
</head>

<body>
<table width="600" border="1" cellspacing="5" cellpadding="5">
  <tr style="background:#CCC">
    <th>Sr No</th>
    <th>Name</th>
    <th>Email</th>
    <th>Mobile</th>
	<th>Delete</th>
	<th>Update</th>
  </tr>
  <?php
  $i=1;
  foreach($data as $row)
  {
  echo "<tr>";
  echo "<td>".$i."</td>";
  echo "<td>".$row->name."</td>";
  echo "<td>".$row->email."</td>";
  echo "<td>".$row->mobile."</td>";
  echo "<td><a href='deletedata?id=".$row->user_id."'>Delete</a></td>";
  echo "<td><a href='updatedata?id=".$row->user_id."'>Update</a></td>";
  echo "</tr>";
  $i++;
  }
   ?>
</table>
</body>
</html>

Update Records View

Copy the below given code in your update_records.php View Page.
<!DOCTYPE html>
<html>
<head>
<title>Registration form</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="name" value="<?php echo $row->name; ?>"/></td>
  </tr>
  <tr>
    <td>Enter Your Email </td>
    <td><input type="text" name="email" value="<?php echo $row->email; ?>"/></td>
  </tr>
  <tr>
    <td>Enter Your Mobile </td>
    <td><input type="text" name="mobile" value="<?php echo $row->mobile; ?>"/></td>
  </tr>
  <tr>
    <td colspan="2" align="center">
	<input type="submit" name="update" value="Update Records"/></td>
  </tr>
</table>
	</form>
	<?php } ?>
</body>
</html>

Model

Copy the below given code in your model.
<?php
class Hello_Model extends CI_Model 
{
	function saverecords($name,$email,$mobile)
	{
	$query="insert into users values('','$name','$email','$mobile')";
	$this->db->query($query);
	}
	
	function displayrecords()
	{
	$query=$this->db->query("select * from users");
	return $query->result();
	}
	
	function deleterecords($id)
	{
	$this->db->query("delete  from users where user_id='".$id."'");
	}
	
	function displayrecordsById($id)
	{
	$query=$this->db->query("select * from users where user_id='".$id."'");
	return $query->result();
	}
	
	function updaterecords($name,$email,$mobile,$id)
	{
	$query=$this->db->query("update users SET name='$name',email='$email',mobile='$mobile' where user_id='".$id."'");
	}	
}
?>

Calling/run Hello Controller’s dispdata method

Open your web browser and Pass : http://localhost/CodeIgniter/index.php/Hello/dispdata
Output(Display Records)
Output(Update Records)

No Sidebar ads