Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
CodeIgniter CRUD Operations with MySQL

CodeIgniter CRUD Operations with MySQL

Posted on June 4, 2018December 17, 2022 By XpertPhp 2 Comments on CodeIgniter CRUD Operations with MySQL

In this tutorial, We will explain to you how to create codeigniter crud operations with mysql. so we will create a simple CRUD application in CodeIgniter with MySQL to perform create (insert), read (select), update, and delete operations.

CRUD stands for create, read, update and delete. if you create a user-friendly website at that time need to crud operation.

Read Also:
Autocomplete Textbox in CodeIgniter using Typeahead

if you want to create CRUD operation in CodeIgniter, then the first configuration of the database and connect the MySQL database then after creating CRUD operation in Codeigniter.

in our post through, We will go how to connect CodeIgniter to MySQL and perform the CRUD operation.

Create Database in table

1
2
3
4
5
6
7
8
9
CREATE TABLE IF NOT EXISTS `register` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(64) NOT NULL,
  `last_name` varchar(64) NOT NULL,
  `address` text NOT NULL,
  `email` varchar(64) NOT NULL,
  `mobile` varchar(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

Connect to Database

Go to config folder and open database.php file some changes in this file like as hostname, database username, database password and database name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'codeigniter_pagination',
'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
);

Create a Register Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Register extends CI_Controller {
public function __construct()
{
parent::__construct();
     $this->load->model('register_model');
 
}
 
public function index()
{
$arrData['register_detail'] = $this->register_model->get_all_register_detail();
$this->load->view('register/list',$arrData);
}
public function add()
{
if($this->input->post('btnadd'))
{
$arrData['first_name'] = $this->input->post('txtFname');
$arrData['last_name'] = $this->input->post('txtLname');
$arrData['address'] = $this->input->post('txtAddress');
$arrData['email'] = $this->input->post('txtEmail');
$arrData['mobile'] = $this->input->post('txtMobile');
 
$insert= $this->register_model->insert($arrData);
if($insert)
{
redirect('register');
}
}
$this->load->view('register/add');
}
public function edit($id)
{
$arrData['register_detail'] = $this->register_model->get_id_wise_register_detail($id);
 
if($this->input->post('btnEdit'))
{
$editData['first_name'] = $this->input->post('txtFname');
$editData['last_name'] = $this->input->post('txtLname');
$editData['address'] = $this->input->post('txtAddress');
$editData['email'] = $this->input->post('txtEmail');
$editData['mobile'] = $this->input->post('txtMobile');
 
$update= $this->register_model->update($editData,$id);
if($update)
{
redirect('register');
}
}
$this->load->view('register/edit',$arrData);
}
public function delete($id)
{
$delete=$this->register_model->delete($id);
if($delete)
{
redirect('register');
}
}
}
?>

Create Register_model Model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_model extends CI_Model
{
public function __construct()
{
$this->load->database('default');
$this->load->library('session');
// Call the Model constructor
parent::__construct();
}
public function get_all_register_detail()
{
$this->db->select('*');
        $this->db->from('register');
        $objQuery = $this->db->get();
        return $objQuery->result_array();
}
public function get_id_wise_register_detail($id)
{
$this->db->select('*');
        $this->db->from('register');
$this->db->where('id',$id);
        $objQuery = $this->db->get();
        return $objQuery->result_array();
}
public function insert($arrData) {
        if ($this->db->insert('register', $arrData)) {
            return true;
        } else {
            return false;
        }
    }
public function update($editData,$id)
{
$this->db->where('id', $id);
 
        if ($this->db->update('register', $editData)) {
            return true;
        } else {
            return false;
        }
}
function delete($id)
{
 
        if($this->db->delete('register', array('id' => $id)))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
 
}
?>
See also  file upload with resize image using codeigniter

Create list.php file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<html>
<head>
<title>codeigniter crud operation with mysql</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script
</head>
<body>
 
<table border="1" align="center">
    <tr>
        <td colspan="5" align="right"><a href="<?php echo base_url(); ?>register/add">Add</a></td>
    </tr>
 
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
<td>Mobile</td>
<td>Action</td>
</tr>
<?php
foreach($register_detail as $rg){
?>
<tr>
<td><?php echo $rg['first_name']; ?></td>
<td><?php echo $rg['last_name']; ?></td>
<td><?php echo $rg['email']; ?></td>
<td><?php echo $rg['mobile']; ?></td>
<td><a  href="<?php echo base_url(); ?>register/edit/<?php echo $rg['id']; ?>">Edit</a>&nbsp;<a  href="<?php echo base_url(); ?>register/delete/<?php echo $rg['id']; ?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>

Create add.php file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<form method="post" name="frmAdd">
        <table align="center">
            <tr>
                <td colspan="2" align="center">Add Record</td>
            </tr>
 
            <tr>
                <td>First Name</td>
                <td><input type="text" name="txtFname"> </td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td><input type="text" name="txtLname"> </td>
            </tr>
            <tr>
                <td>Address</td>
                <td><textarea name="txtAddress" rows="4" cols="16"></textarea> </td>
            </tr>
            <tr>
                <td>Email</td>
                <td><input type="text" name="txtEmail"> </td>
            </tr>
            <tr>
                <td>Mobile</td>
                <td><input type="text" name="txtMobile"> </td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Add" name="btnadd"> </td>
            </tr>
        </table>
</form>

Create edit.php file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<form method="post" name="frmEdit">
<table align="center">
<tr>
<td colspan="2" align="center">Edit Record</td>
</tr>
 
<tr>
<td>First Name</td>
<td><input type="text" name="txtFname" value="<?php echo $register_detail[0]['first_name']; ?>"> </td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="txtLname" value="<?php echo $register_detail[0]['last_name']; ?>"> </td>
</tr>
<tr>
<td>Address</td>
<td><textarea name="txtAddress" rows="4" cols="16"><?php echo $register_detail[0]['address']; ?></textarea> </td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail" value="<?php echo $register_detail[0]['email']; ?>"> </td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="txtMobile" value="<?php echo $register_detail[0]['mobile']; ?>"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Edit" name="btnEdit"> </td>
</tr>
</table>
</form>

We think would you like this article, so you can click on the “Show Demo” button and you can see this demo article.

Show Demo

Codeigniter, MySql Tags:ci tutorial, Codeigniter crud, CodeIgniter crud operation with MySQL, codeigniter tutorial, CodeIgniter tutorial for beginners, codeigniter with mysql, crud operation, learn CodeIgniter

Post navigation

Previous Post: Autocomplete Textbox in CodeIgniter using Typeahead
Next Post: How To Use Multiple Database In Codeigniter

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Angular 15 CRUD Application Example Tutorial
  • Laravel 10 Form Validation Example Tutorial
  • Angular 15 Custom Form Validation Example
  • Laravel 10 Send Email Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com