Codeigniter Delete Query with example – onlinecode

Codeigniter Delete Query with example – onlinecode

In this post we will give you information about Codeigniter Delete Query with example – onlinecode. Hear we will give you detail about Codeigniter Delete Query with example – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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

so you can see the following two ways for Codeigniter delete 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 a delete() Query method. so here we can directly pass data without a model. 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->database();
    }       
    public function delete_product($id) {	  
                if($this->db->delete('product',array('id'=>$id)))
		{
			echo "Product has been successfully deleted";
		}
		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 a delete() 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 delete_product($id) {	  	
		$res = $this->product_model->delete_product('product', array('id' => $id));
		if($res)
		{
			echo "Product has been successfully deleted";
		}
		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 delete_product($table,$where)
    {

        if($this->db->delete($table,$where))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
?>

Please follow and like us:

Hope this code and post will helped you for implement Codeigniter Delete Query with example – 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 *

3  +  4  =  

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