Update Query in Codeigniter using Where Condition

Here, We displayed the best ways to update query in Codeigniter using where condition. Also, given an example of multiple where condition.

Similarly, Codeigniter has to provide a simple query to replace data from the MySQL database using a clause. This is update query $this->db->replace();

public function update_row(){
		
		$update_rows = array('field-name' => 'field-data',);
		$this->db->where('id', 1 );
		$this->db->update('table-name', $update_rows);	

	}	

Thus, I have to show an update function. This function created some elements for the edit query using where condition.

Also, We have to apply a simple update query in Codeigniter.

$result = "update users SET name='Rohan',email='[email protected]' where id='".1."'";
$qyt = $this->db->query($result);

Update Query in Codeigniter using Where Condition

Additionally, you learn more edit queries with the example using a clause. As well as, in this section explain one by one all points.

Firstly, show the code of update query using CI.

public function update_row(){
		
		$update_rows = array('name' => 'rincky',);
		$this->db->where('id', 1 );
		$this->db->update('user', $update_rows);	

	}	

In this section, displaying an example of edit data code. Firstly, create an array and then store array data a variable $update_rows.

Secondly, $this->db->where(‘id’, 1 ); use this function for where conditions.

This clause has given two-parameter which denotes the first field. and second field data likewise id = 1.

After that, use another main codeigniter query to update data $this->db->update(‘user’, $update_rows);.

Afterward, you can see in this update query also pass two-parameter.

Therefore, the first parameter denotes the table name. And second which is denoted array data. which we want to update table row using MySql database with it.

So, all the above section as we discuss this is only for using this code model file. Next, we have to show you the controller part.

update query in codeigniter using where condition

Here, you can see this image showing the updated date. There are multiple rows of data we inserted manually.

So then, apply an ‘update query with where condition’. Through this, we can do edit the data of the database table.

Now, display User interface means output which can be easily understood.

Because there has a table form in which added fields data to retrieve data from the database.

codeigniter update query

Here, this is the table that displays multiple raws data. As an example, use to update query in Codeigniter using where condition.

Edit and Update Data in Codeigniter

Similarly, we discuss in this section about the controller. Actually, there have added some functions for getting the model. And return data with the help of this edited code using CI.

public function update(){
		$this->load->database();
		 $this->load->model('UserModel');
		 $this->UserModel->update_row();		
		
	}

Hence, this function used to fetch data from the model file. Firstly, the load database as a rule of Codeigniter.

After that, access the model then function in this process load model function.

Afterward, the next step to implement for view part. So, through the controller, you can pass updated data to view.

As much as, there is an Html code to design the table and show data in the table.

So, start to get a variable that is a pass to the controller. and then according to functionality access data.

Here, we were given the Html code check the below section of code.

<!DOCTYPE html>
<html>
<head>
	<title>User Data with Project</title>
</head>
<body>
	<h1>Update Query in Codeigniter using where condition</h1>
<table border="2px solid #000 ">
	<tr>
		<td>Name</td>
		<td>Address</td>	
	</tr>
	<?php 	foreach ($users as $userdata) { ?>

<tr>
	<td><?php echo $userdata->name; ?></td>
	<td><?php echo $userdata->address; ?></td>	
		
	
</tr>

<?php } ?>

</table>
</body>
</html>

Again, there is a variable to get from the controller. and created a loop to show the data namely $userdata. Finally, we cover about Codeigniter update query with the example.

Here, The main points to Codeigniter following this the best ways to learn.

Codeigniter Update Query with Array

public function update_row(){
		
		$update_rows = array(
			'name' => 'rincky',
			'address' => 'India',
			'contact' => '98545785',
			'department' => 'IT',

		);
		$this->db->where('id', 1 );
		$this->db->update('user', $update_rows);	

	}	

We have to show you an example of a Codeigniter update query with the array. So, there is a multidimensions array.

In which array has multiple data. Also, store array a variable and then apply an update query to store the data.

Codeigniter Update Query Return Value

public function update_row(){
		
		$update_rows = array(
			'name' => 'rincky',
			'address' => 'India',
			'contact' => '98545785',
			'department' => 'IT',

		);
		$this->db->where('id', 1 );
		$result = $this->db->update('user', $update_rows);	
		return $result;
	}	

Similarly, for the return value, we store query response a variable. The return comes true and false.

After we can add this return response in the controller. and apply a condition for it.

Update Query in Codeigniter Using Multiple Where Condition

public function update_row(){
		
		$update_rows = array('name' => 'rincky');
		$multipleWhere = array('id' => $ids, 'name' => $name, 'status' => 1 );

		$this->db->where($multipleWhere);		
		$this->db->update('user', $update_rows);	
		
	}	
$multipleWhere = array('id' => $ids, 'name' => $name, 'status' => 1 );

		$this->db->where($multipleWhere);

Firstly, we created an array for multiple where conditions. Therefore, you can see above a variable $multiplewhere.

update query in codeigniter using multiple where condition

So then, added multiple values in an array. As well as, Codeigniter given an option to mention.

So, Array is where function like it $this->db->where($multipleWhere);

Conclusion

Finally, displaying an example and code of update query in Codeigniter using where condition.

Also, multiple where conditions you can check the above code available example.

So, If you have any issues in this way to ask us with the comment section. Also, you can learn the most important basic topic of CodeIgniter Tutorial

One Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

CODEIGNITER

Recent Posts

Related Post

How to use Multiple Where Condition in Codeigniter?

Here, we displaying the best method to use multiple where condition in Codeigniter. Also, displaying another method get_where() and multiple where clause in Codeigniter. As well as, given an example fetch data from the database using multiple where condition. likewise, insert, update, and delete. It means all Codeigniter queries with used multiple where clause. Again, […]

Read more

How to Delete Query with Where Condition in Codeigniter

I am showing the best method to learn of delete query with where condition in Codeigniter. As well as, explain Codeigniter delete query return value from the database. Also, given some example code of delete query. when using where condition in the below section. Delete Query with Where Condition in Codeigniter Here, We displaying a […]

Read more

How to use Join Query in Codeigniter

So, I am going to show you how to use a join query in Codeigniter. Also, retrieve the data as you want with example. Here, we have defined many ways to use join query with where condition. As well as, fetch data multiple methods like left join, right join, full join, and inner join in […]

Read more

x