Update Query In Codeigniter Example Tutorial

Update Query In Codeigniter Example Tutorial

Today, We want to share with you Update Query In Codeigniter Example Tutorial.In this post we will show you insert query in codeigniter, hear for select query in codeigniter we will give you demo and example for implement.In this post, we will learn about Select Query In Codeigniter Example Tutorial with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Update Query In Codeigniter Example Tutorial

There are the Following The simple About Codeigniter Active Record: Insert, Select, Update, Delete Full Information With Example and source code.

As I will cover this Post with live Working example to develop delete query in codeigniter, so the join query in codeigniter is used for this example is following below.

PHP Based CodeIgniter Freamworks Like ‘UPDATE’ query statement will run/execute using following methods. They are

Update Query In Codeigniter Example Tutorial
Update Query In Codeigniter Example Tutorial

In this Example, we will all about how to Update records. I will use docators table to update all the Databse records.

  • $this->db->update()
  • $this->db->update_string()
  • $this->db->update_batch()

Codeigniter (CRUD) Select Insert Update Delete Query

  1. Update Query In Codeigniter
  2. Select Query In Codeigniter
  3. Insert Query In Codeigniter
  4. Delete Query In Codeigniter

using $this->db->update()

$data = array( 
    'name'	= >  $_POST['name'] , 
    'teamname'= >  $_POST['teamname'], 
    'rate'	= >  $_POST['rate']
);
$this->db->where('id', $_POST['id']);
$this->db->update('tbl_docator', $data);

using $this->db->update_string()

Imp Note: All the Values are automatically restricted escaped, producing safer statements.

$data = array(
    'teamname'= >$_POST['teamname']
);
$where = "status = 'docator'"; 
$str = $this->db->update_string('tbl_docator', $data, $where);

using $this->db->update_batch()

$data = array(
    array(
        'name'	= >  'name1' , 
        'teamname'= >  'teamname1', 
        'rate'	= >  'rate1'
    ),
    array(
        'name'	= >  'name2' , 
        'teamname'= >  'teamname2', 
        'rate'	= >  'rate2'
    )
);
$this->db->update_batch('tbl_docator', $data); 

Update record CodeIgniter framework PHP

CodeIgniter\application\controllers\Member.php

<?php  
class Member extends CI_Controller 
{
	public function __construct()
	{

	parent::__construct();
	
	$this->load->database();
	
	$this->load->model('Member_model');
	}
	public function dispdata()
	{
	$result['data']=$this->Hello_model->members_list();
	$this->load->view('members_list',$result);
	}
	public function updatedata()
	{
	$id=$this->input->get('id');
	$result['data']=$this->Member_model->displayrecordsById($id);
	$this->load->view('update_records',$result);
	
		if($this->input->post('update'))
		{
		$username=$this->input->post('username');
		$sirname=$this->input->post('sirname');
		$email=$this->input->post('email');
		$this->Member_model->update_records($username,$sirname,$email,$id);
		echo "Date updated successfully !”;
		}
	}
	
}
?>

CodeIgniter\application\models\Member_model.php

<?php
class Member_model extends CI_Model 
{
	function members_list()
	{
		$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();
	}

	function update_records($username,$sirname,$email,$id)
	{
		$query=$this->db->query("update form SET username='$username',sirname='$sirname',email='$email' where id='$id’”);
	}
	
}

CodeIgniter\application\views\members_list.php

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

CodeIgniter\application\views\update_records.php

<!DOCTYPE html>
<html>
<head>
<title>Update Member Data</title>
</head>
 
<body>
 <?php
  $i=1;
  foreach($data as $member)
  {
  ?>
	<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="username" value="<?php echo $member->username; ?>"/></td>
  </tr>
  <tr>
    <td>Enter Your Email </td>
    <td><input type="text" name="sirname" value="<?php echo $member->sirname; ?>"/></td>
  </tr>
  <tr>
    <td>Enter Your Mobile </td>
    <td><input type="text" name="email" value="<?php echo $member->email; ?>"/></td>
  </tr>
  <tr>
    <td colspan="2" align="center">
	<input type="submit" name="update" value="Update_Records"/></td>
  </tr>
</table>
	</form>
	<?php } ?>
</body>
</html>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about codeigniter CRUD query.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Update query in codeigniter Example
  2.   Count Query Codeigniter Tutorial
  3.   Laravel cURL Send HTTP Request Example
  4.   Top 10 Ways to Increase Google Adsense Earnings
  5.   Same Scope Variable in Multiple Controllers using Angular Example
  6.   codeigniter print last query - PHP Examples
  7.   View Files with FileZilla - filezilla edit file on server
  8.   Event Directives with Parameters using Angular Example
  9.   join query in codeigniter Example Tutorial
  10.   Laravel Check Array Empty in Blade Example