Codeigniter Update Query Using Where Condition – onlinecode

Codeigniter Update Query Using Where Condition – onlinecode

In this post we will give you information about Codeigniter Update Query Using Where Condition – onlinecode. Hear we will give you detail about Codeigniter Update Query Using Where Condition – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this article, we will discuss about how to update a record or data (Codeigniter Update Query with example) from the MySQL database using the CodeIgniter framework. so we can easily update data into the database using the Update Query method.

so you can see the following two ways for Codeigniter update database record.

Without Use ModelIn this step, first, we will load the database library in the controller or directly load in the “application/config/autoload.php” file and pass the data into an update() Query method. so here we can directly pass data without a model. see the following below example.

application/controller/Product.php

<?php
 
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Product extends CI_Controller {
 
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }       
    public function update_product($id) {	  
		
		$editData['title'] = $this->input->post('txtTitle');
		$editData['category'] = $this->input->post('txtCategory');
		$editData['description'] = $this->input->post('txtDescription');
		$this->db->where('id', $id);	 
		if ($this->db->update('product', $editData)) {
			echo "Product has been successfully updated";
		} else {
			echo "Something Went Wrong";
		}
    }

}
?>

With Use ModelIn this step, first, we will load the database library in the model or directly load in the “application/config/autoload.php” file and pass the data into an update() Query method. so you can see following below example.

application/controller/Product.php

<?php
 
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Product extends CI_Controller {
 
    public function __construct() {
        parent::__construct();
        $this->load->model('product_model');
    }       
    public function update_product($id) {	  	
		$editData['title'] = $this->input->post('txtTitle');
		$editData['category'] = $this->input->post('txtCategory');
		$editData['description'] = $this->input->post('txtDescription');	 
		if ($this->product_model->update_product($editData,$id)) {
			echo "Product has been successfully updated";
		} else {
			echo "Something Went Wrong";
		}
		
    }
}
?>

application/models/Product_model.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Product_model extends CI_Model {

    public function __construct() {

        parent::__construct();
        $this->load->database();

    }
    public function update_product($editData,$id)
    {
        $this->db->where('id', $id);
        if ($this->db->update('product', $editData)) {
            return true;
        } else {
            return false;
        }
    }
}
?>

Please follow and like us:

Hope this code and post will helped you for implement Codeigniter Update Query Using Where Condition – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  89  =  99

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US