Simplify updates for multiple records in CodeIgniter 4 with our efficient update batch function, streamlining your web development workflow. To change the data of any row in the database, we use the update function. With this we can update all the rows simultaneously and can also update with unique id.
Replace() function primaries. Which ever we set as primary. Its base is updated.
public function update_record()
{
$data = [
'id' => '43',
'name' => 'deepak tailor',
'age' => 201,
'salary'=> 5400
];
// $this->seller database connection
$query = $this->seller->table('users')
->replace($data);
if($query == true){
echo "data replace";
}
else{
echo "data fail";
}
}
We use the set function to set the value in a column like this: we have a column of salary to add an instant 500 to it, then we will set it to the set method.
public function update_record()
{
$query = $this->seller->table('users')
->set('salary','salary + 500',FALSE)
->update();
if($query == true){
echo "data update";
}
else{
echo "data fail";
}
}
public function update_record()
{
$query = $this->seller->table('users')
->set('salary','salary + 5500',FALSE)
->where('id',29)
->update();
if($query == true){
echo "data update";
}
else{
echo "data fail";
}
}
public function update_record()
{
$data = [
'name' => 'DEEPAK TAILOR'
];
$query = $this->seller->table('users')
->where('id',29)
->update($data);
if($query == true){
echo "data update";
}
else{
echo "data fail";
}
}
With updateBatch function, we can update multiple rows. The column to which we set the index in updateBatch. Data is updated on the same column base.
public function update_record()
{
$data = [
[
'id' => 29,
'name' => 'DEEPAK',
'salary'=> 5000
],
[
'id' => 28,
'name' => 'ARJUN',
'salary'=> 8000
]
];
$query = $this->seller->table('users')
->updateBatch($data,'id');
if($query == true){
echo "data update";
}
else{
echo "data fail";
}
}
My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in