Codeigniter 3 – Basic CRUD application with MySQL Example with Demo {codeigniter crud}

codeigniter crud – Codeigniter 3 CRUD Operation with Bootstrap and MySQL Example functionality with Bootstrap and MySQL Example. A CodeIgniter CRUD application is one that uses forms to get data into and out of a mysql database. CRUD is an acronym for Create, Read, Update, and Delete. CRUD operations are basic data manipulation for database.

Codeigniter 3 – Basic CRUD Operation with MySQL Database with example

In this codeigniter crud tutorial i will learn about CRUD operation in PHP CodeIgniter. CRUD Stands for create, read, update and delete record in the MySQL database.

CodeIgniter CRUD Example Tutorial From Scratch
CodeIgniter CRUD Example Tutorial From Scratch

Step 1: Download Codeigniter App

First of all i need to free download the Codeigniter 3 source code just go the PHP Based Codeigniter website as well as free 100% Download. And then downloading extract the folder as well as add it in your local system like path as a xampp/htdocs/.

Step 2: Basic Configurations for codeigniter crud

If you are using virtual host then add Or if you run the project with folder name

$config['base_url'] = 'https://www.pakainfo.com';
//OR
$config['base_url'] = 'http://localhost/codeigniter-crud'; 

Step 3: Setup Database Credential

application/config/database.php

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'admin',
    'database' => 'product_data',
    '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
);

Step 3: Create Database Table

And then execute following simple SQL Query with selecting the database in phpmyadmin and make β€œshops” table for codeigniter crud.

CREATE TABLE `paakinfo_v1` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255),
 `information` text,
 PRIMARY KEY (`id`)
)  

Step 4: Create Routes

application\config\routes.php

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

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

$route['shop'] = "Shop/index";
$route['shop/create'] = "Shop/create";
$route['shop/edit'] = "Shop/edit/$1";
$route['shop/store'] = "Shop/store";
$route['shop/show'] = "Shop/show/$1";
$route['shop/update/(:any)'] = "Shop/update/$1";
$route['shop/delete'] = "Shop/delete";

Step 5: Create Controller

application\controllers\Shop.php

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

class Shop extends CI_Controller {

    function __construct(){
      parent::__construct();
      $this->load->helper('url');
      $this->load->library('session');
      $this->load->database();
    }

  public function index()
  {
    $shops = $this->db->get('shops')->result();
    $this->load->view('shop/index', ['shops' => $shops]);
  }

  public function create()
  {

    $this->load->view('shop/create');
  }

  public function edit($id)
  {
    $shop = $this->db->where(['id' => $id])->get('shops')->row();
    $this->load->view('shop/edit', ['shop' => $shop]);
  }

  public function store()
  {
      $this->load->library('form_validation');
      $this->form_validation->set_rules('name', 'Name', 'required');
      $this->form_validation->set_rules('information', 'Information','required');

      if ($this->form_validation->run()) {
        $shop = array (
          'name' => $this->input->post('name'),
          'information' => $this->input->post('information'),
        );

        $this->db->insert('shops', $shop);
      } else {
        $errors = $this->form_validation->error_array();
        $this->session->set_flashdata('errors', $errors);
        redirect(base_url('shop/create'));
      }

      redirect('/shop');
  }

  public function update($id)
  {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('information', 'Information', 'required');

    if ($this->form_validation->run()) {
      $shop = array (
        'name' => $this->input->post('name'),
        'information' => $this->input->post('information'),
      );

       $this->db->where(['id' => $id])->update('shops', $shop);
    } else {
      $errors = $this->form_validation->error_array();
      $this->session->set_flashdata('errors', $errors);
      redirect(base_url('shop/edit/'. $id));
    }

     redirect('/shop');
  }

  public function show($id) {
     $shop = $this->db->where(['id' => $id])->get('shops')->row();
     $this->load->view('shop/show',['shop' => $shop]);
  }

  public function delete($id)
  {
     $this->db->where(['id' => $id])->delete('shops');

     redirect('/shop');
  }

}

Step 6: Create Views

application\views\shop\index.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">
    <div class="row">
      <div class="col-lg-12 margin-tb">
          <div class="pull-left">
              <h2>Codeigniter 3 CRUD Example from scratch with <a href="https://www.pakainfo.com/">pakainfo</a></h2>
          </div>
          <div class="pull-right">
              <a class="btn btn-success" href="/shop/create"> Create New Item</a>
          </div>
      </div>
    </div>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Name</th>
                <th>Information</th>
                <th width="220px">Action</th>
            </tr>
        </thead>
        <tbody>
          <?php foreach ($shops as $shop) { ?>
            <tr>
                <td><?php echo $shop->name; ?></td>
                <td><?php echo $shop->information; ?></td>
            <td>
              <form method="DELETE" action="<?php echo base_url('shop/delete/'.$shop->id);?>">
                <a class="btn btn-info" href="<?php echo base_url('shop/show/'.$shop->id) ?>"> show</a>
               <a class="btn btn-primary" href="<?php echo base_url('shop/edit/'.$shop->id) ?>"> Edit</a>
                <button type="submit" class="btn btn-danger"> Delete</button>
              </form>
            </td>
            </tr>
          <?php } ?>
        </tbody>
    </table>
  </div>
</body>
</html>

Creating New shop

For making fresh shop i need to show the form using the create.php file, make and put the below source code inside the file.
application\views\shop\create.php

<!DOCTYPE html>
<html>
<head>
    <title>Basic Crud operation in Codeigniter 3 - codeigniter crud</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
  <div class="container">
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Create Shop</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="<?php echo base_url('shop')?>"> Back</a>
            </div>
        </div>
    </div>
    <form method="post" action="<?php echo base_url('shop/store')?>">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    <input type="text" name="name" class="form-control">
                    <?php
                      if (isset($this->session->flashdata('errors')['name'])){
                          echo '<div class="alert alert-danger mt-2">';
                          echo $this->session->flashdata('errors')['name'];
                          echo "</div>";
                      }
                    ?>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Information:</strong>
                    <textarea name="information" class="form-control"></textarea>
                    <?php
                      if (isset($this->session->flashdata('errors')['information'])){
                          echo '<div class="alert alert-danger mt-2">';
                          echo $this->session->flashdata('errors')['information'];
                          echo "</div>";
                      }
                    ?>
                </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>

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

Edit the shop

Now edit the shop using the edit.php file.
application\views\shop\edit.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">
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Update Shop</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-primary" href="<?php echo base_url('shop')?>"> Back</a>
            </div>
        </div>
    </div>
    <form method="post" action="<?php echo base_url('shop/update/'.$shop->id)?>">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Name:</strong>
                    <input type="text" name="name" class="form-control" value="<?php echo $shop->name; ?>">
                    <?php
                      if (isset($this->session->flashdata('errors')['name'])){
                          echo '<div class="alert alert-danger mt-2">';
                          echo $this->session->flashdata('errors')['name'];
                          echo "</div>";
                      }
                    ?>
                </div>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-12">
                <div class="form-group">
                    <strong>Information:</strong>
                    <textarea name="information" class="form-control"><?php  echo $shop->information; ?></textarea>
                    <?php
                      if (isset($this->session->flashdata('errors')['information'])){
                          echo '<div class="alert alert-danger mt-2">';
                          echo $this->session->flashdata('errors')['information'];
                          echo "</div>";
                      }
                    ?>
                </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>

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

Showing the shop

Showing the shop we need to create a show.php file.
application\views\shop\show.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">
    <div class="row">
      <div class="col-lg-12 margin-tb">
          <div class="pull-left">
              <h2>Codeigniter 3 CRUD Example from scratch with <a href="https://www.pakainfo.com/">pakainfo</a></h2>
          </div>
          <div class="pull-right">
              <a class="btn btn-success" href="/shop"> Back</a>
          </div>
      </div>
    </div>

    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Name:</strong>
                <?php echo $shop->name; ?>
            </div>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>Information:</strong>
                <?php echo $shop->information; ?>
            </div>
        </div>
    </div>

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

Conclusion – codeigniter crud

In this codeigniter basic crud article with Codeigniter Config – I have successfully created step by step first basic crud (create, update, read, delete) full source code using mysql database.