CodeIgniter CRUD Tutorial: Create, Read, Update, and Delete Data

Codeigniter 3 – Basic CRUD application with MySQL Example with Demo

Step 1: codeigniter download
here simple you can codeigniter download Step 1: Download Codeigniter 3

Step 2: Create a Database and Configuration

products table:

CREATE TABLE IF NOT EXISTS `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`product_short_desc` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;

Example 3: application/config/database.php

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


$active_group = 'default';
$query_builder = TRUE;


$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'jay_dsp',
'password' => 'Jaydeep@344895754',
'database' => 'pakainfo',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

codeigniter route crud (Step 3: Create Routes)

application/config/routes.php
here you can simply codeigniter route crud

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


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;


$route['productCRUDExample'] = "productCRUDExample/index";
$route['productCRUDExample/(:num)'] = "productCRUDExample/show/$1";
$route['productCRUDExampleCreate']['post'] = "productCRUDExample/store";
$route['productCRUDExampleEdit/(:any)'] = "productCRUDExample/edit/$1";
$route['productCRUDExampleUpdate/(:any)']['put'] = "productCRUDExample/update/$1";
$route['productCRUDExampleDelete/(:any)']['delete'] = "productCRUDExample/delete/$1";

Step 4: Add ProductCRUDExample Controller

application/controllers/ProductCRUDExample.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ProductCRUDExample extends CI_Controller {


public $productCRUDExample;


/**
* Get All Data from this method.
*
* @return Response
*/
public function __construct() {
parent::__construct();


$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('ProductCRUDExampleModel');


$this->productCRUDExample = new ProductCRUDExampleModel;
}


/**
* Display Data this method.
*
* @return Response
*/
public function index()
{
$data['data'] = $this->productCRUDExample->get_productCRUDExample();


$this->load->view('theme/header');
$this->load->view('productCRUDExample/list',$data);
$this->load->view('theme/footer');
}


/**
* Show Details this method.
*
* @return Response
*/
public function show($id)
{
$product = $this->productCRUDExample->find_product($id);


$this->load->view('theme/header');
$this->load->view('productCRUDExample/show',array('product'=>$product));
$this->load->view('theme/footer');
}


/**
* Create from display on this method.
*
* @return Response
*/
public function create()
{
$this->load->view('theme/header');
$this->load->view('productCRUDExample/create');
$this->load->view('theme/footer');
}


/**
* Store Data from this method.
*
* @return Response
*/
public function store()
{
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('product_short_desc', 'Description', 'required');


if ($this->form_validation->run() == FALSE){
$this->session->set_flashdata('errors', validation_errors());
redirect(base_url('productCRUDExample/create'));
}else{
$this->productCRUDExample->insert_product();
redirect(base_url('productCRUDExample'));
}
}


/**
* Edit Data from this method.
*
* @return Response
*/
public function edit($id)
{
$product = $this->productCRUDExample->find_product($id);


$this->load->view('theme/header');
$this->load->view('productCRUDExample/edit',array('product'=>$product));
$this->load->view('theme/footer');
}


/**
* Update Data from this method.
*
* @return Response
*/
public function update($id)
{
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('product_short_desc', 'Description', 'required');


if ($this->form_validation->run() == FALSE){
$this->session->set_flashdata('errors', validation_errors());
redirect(base_url('productCRUDExample/edit/'.$id));
}else{
$this->productCRUDExample->update_product($id);
redirect(base_url('productCRUDExample'));
}
}


/**
* Delete Data from this method.
*
* @return Response
*/
public function delete($id)
{
$product = $this->productCRUDExample->delete_product($id);
redirect(base_url('productCRUDExample'));
}
}

Step 5: Create ProductCRUDExample Model

application/models/ProductCRUDExample.php

<?php


class ProductCRUDExampleModel extends CI_Model{


public function get_productCRUDExample(){
if(!empty($this->input->get("search"))){
$this->db->like('title', $this->input->get("search"));
$this->db->or_like('product_short_desc', $this->input->get("search"));
}
$query = $this->db->get("products");
return $query->result();
}


public function insert_product()
{
$data = array(
'title' => $this->input->post('title'),
'product_short_desc' => $this->input->post('product_short_desc')
);
return $this->db->insert('products', $data);
}


public function update_product($id)
{
$data=array(
'title' => $this->input->post('title'),
'product_short_desc'=> $this->input->post('product_short_desc')
);
if($id==0){
return $this->db->insert('products',$data);
}else{
$this->db->where('id',$id);
return $this->db->update('products',$data);
}
}


public function find_product($id)
{
return $this->db->get_where('products', array('id' => $id))->row();
}


public function delete_product($id)
{
return $this->db->delete('products', array('id' => $id));
}
}
?>

Step 6: Create View Files

  • 1) header.php
  • 2) footer.php
  • 3) list.php
  • 4) create.php
  • 5) show.php
  • 6) edit.php

application/views/theme/header.php

<!DOCTYPE html>
<html>
<head>
<title>Basic Crud operation in Codeigniter 3</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<div class="container">

application/views/theme/footer.php

</div>
</body>
</html>

application/views/productCRUDExample/list.php

<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Codeigniter 3 CRUD Example from scratch</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="<?php echo base_url('productCRUDExample/create') ?>"> Create New Product</a>
</div>
</div>
</div>


<table class="table table-bordered">


<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th width="220px">Action</th>
</tr>
</thead>


<tbody>
<?php foreach ($data as $product) { ?>
<tr>
<td><?php echo $product->title; ?></td>
<td><?php echo $product->product_short_desc; ?></td>
<td>
<form method="DELETE" action="<?php echo base_url('productCRUDExample/delete/'.$product->id);?>">
<a class="btn btn-info" href="<?php echo base_url('productCRUDExample/'.$product->id) ?>"> show</a>
<a class="btn btn-primary" href="<?php echo base_url('productCRUDExample/edit/'.$product->id) ?>"> Edit</a>
<button type="submit" class="btn btn-danger"> Delete</button>
</form>
</td>
</tr>
<?php } ?>
</tbody>


</table>

application/views/productCRUDExample/create.php

<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo base_url('productCRUDExample');?>"> Back</a>
</div>
</div>
</div>


<form method="post" action="<?php echo base_url('productCRUDExampleCreate');?>">
<?php


if ($this->session->flashdata('errors')){
echo '<div class="alert alert-danger">';
echo $this->session->flashdata('errors');
echo "</div>";
}


?>


<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
<input type="text" name="title" class="form-control">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
<textarea name="product_short_desc" class="form-control"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>


</form>

application/views/productCRUDExample/show.php

<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo base_url('productCRUDExample');?>"> Back</a>
</div>
</div>
</div>


<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
<?php echo $product->title; ?>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
<?php echo $product->product_short_desc; ?>
</div>
</div>
</div>

application/views/productCRUDExample/edit.php

<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="<?php echo base_url('productCRUDExample');?>"> Back</a>
</div>
</div>
</div>


<form method="post" action="<?php echo base_url('productCRUDExample/update/'.$product->id);?>">
<?php


if ($this->session->flashdata('errors')){
echo '<div class="alert alert-danger">';
echo $this->session->flashdata('errors');
echo "</div>";
}


?>


<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Title:</strong>
<input type="text" name="title" class="form-control" value="<?php echo $product->title; ?>">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Description:</strong>
<textarea name="product_short_desc" class="form-control"><?php echo $product->product_short_desc; ?></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>


</form>

application/config/config.php

$config['base_url'] = 'http://localhost:8000';

I hope you get an idea about Multiple Inserts, Update, Delete using Multiple Select in Codeigniter.


#codeigniter #crud 

CodeIgniter CRUD Tutorial: Create, Read, Update, and Delete Data
1.55 GEEK