PHP Codeigniter 3 - Basic CRUD Operation with MySQL Database with example

PHP Codeigniter 3 - Basic CRUD Operation with MySQL Database with example

Codeigniter 3 - Basic CRUD Operation with MySQL Database with example

In this tutorial, I will tell you the basic CRUD operation with MySQL database with example in Codeigniter 3.

You will find the step by step process to build a simple application having crud functionality in Codeigniter 3 with MySQL Database.

CRUD operation is a common thing in the application of familiarity with basic create, read, update and delete functionality of the Database.

Step 1: Install Codeigniter 3

Codeigniter 3 is one of the most popular PHP Framework and you can easily install it in your system.

You can easily download it from here : Download Codeigniter 3

As we know, CodeIgniter is very lightweight framework so it will not take long time to download.

After download successfully, extract it in your working directory where you run your PHP script.

Step 2: Database Configuration

To perform crud operation, We will need to have a database with tables so first I will create a demo database and then create a products table within the database.

Run the following sql query to create a table :

CREATE TABLE `products` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(255) NOT NULL,
 `description` varchar(255) NOT NULL,
 `created_at` datetime NOT NULL,
 `updated_at` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

Now I have a products table on which i will perform the crud functionality.

Ok, let's configure the database with codeigniter application so open the database.php file and put your database credentials (database name, username and password) there.

You will find the database.php file in following path application/config/database.php.

  1. $active_group = 'default';
  2. $query_builder = TRUE;
  3. $db['default'] = array(
  4.     'dsn'    => '',
  5.     'hostname' => 'localhost',
  6.     'username' => 'root',
  7.     'password' => 'xxxx',
  8.     'database' => 'demo',
  9.     'dbdriver' => 'mysqli',
  10.     'dbprefix' => '',
  11.     'pconnect' => FALSE,
  12.     'db_debug' => (ENVIRONMENT !== 'production'),
  13.     'cache_on' => FALSE,
  14.     'cachedir' => '',
  15.     'char_set' => 'utf8',
  16.     'dbcollat' => 'utf8_general_ci',
  17.     'swap_pre' => '',
  18.     'encrypt' => FALSE,
  19.     'compress' => FALSE,
  20.     'stricton' => FALSE,
  21.     'failover' => array(),
  22.     'save_queries' => TRUE
  23. );
Step 3: Route

In this step, I will add routes in application/congif/routes.php file to handle the request.

  1. $route['default_controller'] = 'welcome';
  2. $route['404_override'] = '';
  3. $route['translate_uri_dashes'] = FALSE;
  4. $route['products'] = "products/index";
  5. $route['productsCreate']['post'] = "products/store";
  6. $route['productsEdit/(:any)'] = "products/edit/$1";
  7. $route['productsUpdate/(:any)']['put'] = "products/update/$1";
  8. $route['productsDelete/(:any)']['delete'] = "products/delete/$1";

In routes "products/index", the first segment will be remapped to the controller class and second will be remapped with the method of that controller.

Step 4: Create Controller

In this step, I will create a controller to handle the method for product listing, create, edit, update and delete.

Now I will create a Products.php file in following path application/controllers/.

  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Products extends CI_Controller {
  4. /**
  5. * Get All Data from this method.
  6. *
  7. * @return Response
  8. */
  9. public function __construct() {
  10. //load database in autoload libraries
  11. parent::__construct();
  12. $this->load->model('ProductsModel');
  13. }
  14. public function index()
  15. {
  16. $products=new ProductsModel;
  17. $data['data']=$products->get_products();
  18. $this->load->view('includes/header');
  19. $this->load->view('products/list',$data);
  20. $this->load->view('includes/footer');
  21. }
  22. public function create()
  23. {
  24. $this->load->view('includes/header');
  25. $this->load->view('products/create');
  26. $this->load->view('includes/footer');
  27. }
  28. /**
  29. * Store Data from this method.
  30. *
  31. * @return Response
  32. */
  33. public function store()
  34. {
  35. $products=new ProductsModel;
  36. $products->insert_product();
  37. redirect(base_url('products'));
  38. }
  39. /**
  40. * Edit Data from this method.
  41. *
  42. * @return Response
  43. */
  44. public function edit($id)
  45. {
  46. $product = $this->db->get_where('products', array('id' => $id))->row();
  47. $this->load->view('includes/header');
  48. $this->load->view('products/edit',array('product'=>$product));
  49. $this->load->view('includes/footer');
  50. }
  51. /**
  52. * Update Data from this method.
  53. *
  54. * @return Response
  55. */
  56. public function update($id)
  57. {
  58. $products=new ProductsModel;
  59. $products->update_product($id);
  60. redirect(base_url('products'));
  61. }
  62. /**
  63. * Delete Data from this method.
  64. *
  65. * @return Response
  66. */
  67. public function delete($id)
  68. {
  69. $this->db->where('id', $id);
  70. $this->db->delete('products');
  71. redirect(base_url('products'));
  72. }
  73. }
Step 4: Create Model

In this step, I will create a model file to define some common functionality that will interact with the database table.

Create a ProductsModel.php file in following path application/models.

  1. <?php
  2. class ProductsModel extends CI_Model{
  3.     
  4. public function get_products(){
  5. if(!empty($this->input->get("search"))){
  6. $this->db->like('title', $this->input->get("search"));
  7. $this->db->or_like('description', $this->input->get("search"));
  8. }
  9. $query = $this->db->get("products");
  10. return $query->result();
  11. }
  12. public function insert_product()
  13. {
  14. $data = array(
  15. 'title' => $this->input->post('title'),
  16. 'description' => $this->input->post('description')
  17. );
  18. return $this->db->insert('products', $data);
  19. }
  20. public function update_product($id)
  21. {
  22.     $data=array(
  23.             'title' => $this->input->post('title'),
  24.             'description'=> $this->input->post('description')
  25.     );
  26.     if($id==0){
  27.         return $this->db->insert('products',$data);
  28.     }else{
  29.         $this->db->where('id',$id);
  30.         return $this->db->update('products',$data);
  31.     }     
  32. }
  33. }
  34. ?>
Step 5: Create View File

In this step, I will create different view files that will be use in this crud application :

  • includes/header.php
  • includes/footer.php
  • products/list.php
  • products/create.php
  • products/edit.php
includes/header.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Basic Crud operation in Codeigniter 3</title>
  5.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
  6. </head>
  7. <body>
  8. <div class="container">
includes/footer.php
  1. </div>
  2. </body>
  3. </html>
products/list.php
  1. <div class="row">
  2.     <div class="col-lg-12">
  3. <h2>Products CRUD
  4. <div class="pull-right">
  5. <a class="btn btn-primary" href="<?php echo base_url('products/create') ?>"> Create New Product</a>
  6. </div>
  7. </h2>
  8. </div>
  9. </div>
  10. <div class="table-responsive">
  11. <table class="table table-bordered">
  12. <thead>
  13.     <tr>
  14.         <th>Title</th>
  15.         <th>Description</th>
  16. <th>Action</th>
  17.     </tr>
  18. </thead>
  19. <tbody>
  20. <?php foreach ($data as $d) { ?>     
  21.     <tr>
  22.         <td><?php echo $d->title; ?></td>
  23.         <td><?php echo $d->description; ?></td>         
  24. <td>
  25. <form method="DELETE" action="<?php echo base_url('products/delete/'.$d->id);?>">
  26. <a class="btn btn-info btn-xs" href="<?php echo base_url('products/edit/'.$d->id) ?>"><i class="glyphicon glyphicon-pencil"></i></a>
  27. <button type="submit" class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-remove"></i></button>
  28. </form>
  29. </td>
  30.     </tr>
  31.     <?php } ?>
  32. </tbody>
  33. </table>
  34. </div>
products/create.php
  1. <form method="post" action="<?php echo base_url('productsCreate');?>">
  2.     <div class="row">
  3.         <div class="col-md-8 col-md-offset-2">
  4.             <div class="form-group">
  5.                 <label class="col-md-3">Title</label>
  6.                 <div class="col-md-9">
  7.                     <input type="text" name="title" class="form-control">
  8.                 </div>
  9.             </div>
  10.         </div>
  11.         <div class="col-md-8 col-md-offset-2">
  12.             <div class="form-group">
  13.                 <label class="col-md-3">Description</label>
  14.                 <div class="col-md-9">
  15.                     <textarea name="description" class="form-control"></textarea>
  16.                 </div>
  17.             </div>
  18.         </div>
  19.         <div class="col-md-8 col-md-offset-2 pull-right">
  20.             <input type="submit" name="Save" class="btn">
  21.         </div>
  22.     </div>
  23.     
  24. </form>
products/edit.php
  1. <form method="post" action="<?php echo base_url('products/update/'.$product->id);?>">
  2.     <div class="row">
  3.         <div class="col-md-8 col-md-offset-2">
  4.             <div class="form-group">
  5.                 <label class="col-md-3">Title</label>
  6.                 <div class="col-md-9">
  7.                     <input type="text" name="title" class="form-control" value="<?php echo $product->title; ?>">
  8.                 </div>
  9.             </div>
  10.         </div>
  11.         <div class="col-md-8 col-md-offset-2">
  12.             <div class="form-group">
  13.                 <label class="col-md-3">Description</label>
  14.                 <div class="col-md-9">
  15.                     <textarea name="description" class="form-control"><?php echo $product->description; ?></textarea>
  16.                 </div>
  17.             </div>
  18.         </div>
  19.         <div class="col-md-8 col-md-offset-2 pull-right">
  20.             <input type="submit" name="Save" class="btn">
  21.         </div>
  22.     </div>
  23.     
  24. </form>

If you will get the error of undefined function base_url() then follow the URL : Call to undefined function base_url() in Codeigniter

Click here to know how to implement CRUD Operation in Laravel PHP Framework :

Laravel CRUD Example

Phone: (+91) 8800417876
Noida, 201301
Deneme bonusu veren siteler Bahis forum Bahis forumu
ataşehir escort maltepe escort escort bayan anadolu yakası escort kaliteli escort anadolu yakası escort çekmeköy escort ümraniye escort kurtköy escort anadolu yakası escort escort bayan bostancı escort ataşehir escort ümraniye escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort ankara escort etlik escort çankaya escort kızılay escort demetevler escort dikmen escort esat escort eryaman escort ankara escort bayan
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort alanya escort manavgat escort bursa escort eskişehir escort sakarya escort samsun escort malatya escort trabzon escort adıyaman escort afyon escort ağrı escort aksaray escort amasya escort ardahan escort artvin escort aydın escort balikesir escort bartın escort batman escort bayburt escort bilecik escort bingöl escort bitlis escort bolu escort burdur escort çanakkale escort çankırı escort çorum escort düzce escort edirne escort elazığ escort erzincan escort erzurum escort giresun escort gümüşhane escort hatay escort ığdır escort ısparta escort maraş escort karabük escort karaman escort kars escort kastamonu escort kilis escort kırıkkale escort kırklareli escort kırşehir escort kütahya escort manisa escort mardin escort muğla escort muş escort nevşehir escort niğde escort ordu escort osmaniye escort rize escort urfa escort siirt escort sinop escort şırnak escort sivas escort ordu escort tokat escort tunceli escort usak escort van escort yalova escort yozgat escort zonguldak escort kıbrıs escort
bedava bahis
atasehir escort anadolu yakasi escort ankara escort
adana escort adapazarı escort afyon escort aksaray escort alanya escort aliağa escort amasya escort antalya escort ataşehir escort aydın escort balıkesir escort bartın escort batman escort bayraklı escort bilecik escort bodrum escort bolu escort bornova escort buca escort burdur escort bursa escort çanakkale escort çankırı escort çorum escort darıca escort didim escort edirne escort edremit escort elazığ escort konya escort eryaman escort kıbrıs escort eskişehir escort fethiye escort giresun escort gümüşhane escort güngören escort hatay escort iskenderun escort ısparta escort gaziemir escort karabük escort karaman escort karşıyaka escort kaş escort kastamonu escort kayseri escort kırıkkale escort kırklareli escort kırşehir escort kocaeli escort kuşadası escort kütahya escort lara escort malatya escort manisa escort maraş escort mersin escort muğla escort nevşehir escort niğde escort ödemiş escort ordu escort osmaniye escort pendik escort sakarya escort atakum escort samsun escort sincan escort sinop escort sivas escort tekirdağ escort tokat escort trabzon escort tuzla escort urfa escort uşak escort düzce escort yalova escort yozgat escort zonguldak escort dörtyol escort antakya escort yüreğir escort bandırma escort gürsu escort marmaris escort ereğli escort çorlu escort afşin escort gemlik escort kemer escort karatay escort selçuklu escort beyşehir escort bosna escort ceyhan escort nazilli escort talas escort yıldırım escort

viagra sipariş

lifta 20 mg

cialis fiyat

viagra 100 mg fiyat

cialis jel

viagra satın al

cialis 5 mg fiyat

viagra satın al

viagra fiyatı

degra

vigrande

degra 100 mg

orcafil

cialis nedir

cialis 20 mg eczane fiyatı

cialis fiyat

cialis 20 mg

cialis 100 mg

viagra fiyat

lifta

lifta 5 mg

hardcis

cialis eczane

cialis 5 mg fiyatı

cialis 100 mg fiyat

cialis 5 mg

viagra fiyatları

viagra satış

online eczane viagra

viagra eczane

cialis 20 mg

cialis 100 mg

cialis hap

cialis 20

cialis eczane

cialis satış

cialis fiyatları

cialis fiyatı

viagra fiyat

viagra fiyatları

viagra eczane

viagra satın al

pfizer viagra satış

online eczane viagra

pfizer viagra satın al

viagra sipariş

viagra sipariş hattı

viagra fiyat 2022

online viagra sipariş

viagra fiyat 2022 eczane

viagra fiyat eczane

cialis 5 mg

kamagra satın al

kamagra

kamagra jel

kamagra jel sipariş

kamagra sipariş

kamagra fiyat

kamagra jel fiyatı

kamagra fiyatı

kamagra 100mg

kamagra 100mg fiyat

süper kamagra

kamagra nedir

Ereksiyon Hapları

orcafil 5 mg

flynta 5 mg

lifta 5 mg

flynta 20 mg

lifta 20 mg

degra fiyat

degra 100 mg

sildegra 100 mg

sildegra

sinegra

vigrande

vigrande 100 mg

combo 100 mg

vigaroo

jeligra

cialis satış

cialis 100 mg fiyat

cialis 20 mg eczane fiyatı

cialis 5 mg

cialis hap

cialis 5 mg fiyatı

cialis nedir

cialis 100 mg

cialis 20 mg

cialis fiyat

cialis

viagra fiyatı 2021

viagra fiyat

viagra yorum

viagra nedir

viagra satın al

viagra sipariş

eczane viagra

viagra 100 mg

orijinal viagra

viagra 100 mg fiyat

novagra

geciktirici krem

geciktirici sprey

viga krem

viga sprey

nely8

stag sprey

delay sprey

geciktirici mendil

geciktirici

izmir escort
ataşehir escort