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='xyz@gmail.com' where id='".1."'";
$qyt = $this->db->query($result);
Table of Contents
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.
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.
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.
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
Like!! I blog frequently and I really thank you for your content. The article has truly peaked my interest.
You made some respectable factors there. I appeared on the internet for the issue and located most people will go along with with your website.
I?d have to check with you here. Which is not something I usually do! I enjoy reading a post that will make people think. Also, thanks for allowing me to comment!