Codeigniter 4 CRUD (Create Read Update Delete) Tutorial

29-May-2020

.

Admin

Hi Dev,

Today, In this tutorial I will share with you how to perform crud operation in codeigniter. We will perform crud opeartion like create read upadte delete operation in codeigniter.

CRUD is basic step of any core language framework. CRUD stands for Create Read Update and Delete. So In this blog we will learn you insert upadte and delete in codeigniter.

if you want to create CRUD operation in CodeIgniter 4, There are listed bellow step you have to follow:

1) Download Codeigniter


2) Create Database and Configuration

3) Add Route

4) Create Controller and Model

5) Create View Files

6) Run Application

Step 1 : Download Codeigniter

In First step we will download fresh version of Codeigniter 4, so if you haven't download yet then download from here : Download Codeigniter 4

After Download successfully, extract clean new Codeigniter 4 application.

Step 2 : Create Database and Configuration

In this step, You will create new database "blog" and add new table "users" in the blog database. You can use following SQL Query for create "users" table. So let's create using bellow sql query:

CREATE TABLE IF NOT EXISTS `users` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

`city` varchar(255) COLLATE utf8_unicode_ci NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;

After create database and table successfully, we have to configuration of database in our Codeigniter 4 application, so open database.php file and add your database name, username and password.

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' => 'root',

'password' => 'root',

'database' => 'blog',

'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 : Add Route

In this step you have to add some route in your route file. So first we will create route for users modules for lists, create, edit, update and delete.so put the bellow content in route file:

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['users'] = "users/index";

$route['usersCreate']['post'] = "users/store";

$route['usersEdit/(:any)'] = "users/edit/$1";

$route['usersUpdate/(:any)']['put'] = "users/update/$1";

$route['usersDelete/(:any)']['delete'] = "users/delete/$1";

Step 4 : Create Controller and Model

In this step you have to add some route in your route file. So first we will create route for users modules for lists, create, edit, update and delete.so put the bellow content in route file:

app/controller/User.php

<php

use CodeIgniter\Controller;

use App\Models\UserModel;

class User extends Controller

{

public function index()

{

$model = new UserModel();

$data['users_detail'] = $model->orderBy('id', 'DESC')->findAll();

return view('list', $data);

}

public function create()

{

return view('add');

}

public function store()

{

helper(['form', 'url']);

$model = new UserModel();

$data = [

'name' => $this->request->getVar('txtName'),

'email' => $this->request->getVar('txtEmail'),

'password' => $this->request->getVar('txtPassword'),

'city' => $this->request->getVar('txtCity'),

];

$save = $model->insert($data);

return redirect()->to( base_url('User') );

}

public function edit($id = null)

{

$model = new UserModel();

$data['user'] = $model->where('id', $id)->first();

return view('edit', $data);

}

public function update()

{

helper(['form', 'url']);

$model = new UserModel();

$id = $this->request->getVar('id');

$data = [

'name' => $this->request->getVar('txtName'),

'email' => $this->request->getVar('txtEmail'),

'password' => $this->request->getVar('txtPassword'),

'city' => $this->request->getVar('txtCity'),

];

$save = $model->update($id,$data);

return redirect()->to( base_url('users') );

}

public function delete($id = null)

{

$model = new UserModel();

$data['user'] = $model->where('id', $id)->delete();

return redirect()->to( base_url('users') );

}

}

?>

app/Models/UserModel.php

<php

use CodeIgniter\Database\ConnectionInterface;

use CodeIgniter\Model;

class UserModel extends Model

{

protected $table = 'users';

protected $allowedFields = ['name','email','password','city'];

}

?>

Step 5 : Create View

app/views/list.php

<html>

<head>

<title>Codeigniter 4 CRUD (Create Read Update Delete) Tutorial - Nicesnippets.com</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 site_url('usersCreate') ?>">Add</a></td>

</tr>

<tr>

<td>Name</td>

<td>Email</td>

<td>Password</td>

<td>City</td>

<td>Action</td>

</tr>

<?php

foreach($users_detail as $value){

?>

<tr>

<td><?php echo $value['name']; ?></td>

<td><?php echo $value['email']; ?></td>

<td><?php echo $value['password']; ?></td>

<td><?php echo $value['city']; ?></td>

<td><a href="<?php echo base_url(); ?>/usersEdit/<?php echo $value['id']; ?>">Edit</a> <a href="<?php echo base_url(); ?>/usersDelete/<?php echo $value['id']; ?>">Delete</a></td>

</tr>

<?php

}

?>

</table>

</body>

</html>

app/views/add.php

<form method="post" name="frmAdd" action="<?php echo site_url('usersCreate');?>">

<table align="center">

<tr>

<td colspan="2" align="center">Add Record</td>

</tr>

<tr>

<td>Name</td>

<td><input type="text" name="txtName"> </td>

</tr>

<tr>

<td>Email</td>

<td><input type="text" name="txtEmail"> </td>

</tr>

<tr>

<td>Password</td>

<td><input type="password" name="txtPassword"> </td>

</tr>

<tr>

<td>City</td>

<td><input type="text" name="txtCity"> </td>

</tr>

<tr>

<td colspan="2" align="center"><input type="submit" value="Add" name="btnadd"> </td>

</tr>

</table>

</form>

app/views/edit.php

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

<table align="center">

<tr>

<td colspan="2" align="center">Edit Record</td>

</tr>

<input type="hidden" name="id" class="form-control" id="id" value="<?php echo $user['id'] ?>">

<tr>

<td>Name</td>

<td><input type="text" name="txtName" value="<?php echo $user['name']; ?>"> </td>

</tr>

<tr>

<td>Email</td>

<td><input type="text" name="txtEmail" value="<?php echo $user['email']; ?>"> </td>

</tr>

<tr>

<td>Password</td>

<td><input type="text" name="txtPassword" value="<?php echo $user['password']; ?>"> </td>

</tr>

<tr>

<td>City</td>

<td><input type="text" name="txtCity" value="<?php echo $user['city']; ?>"> </td>

</tr>

<tr>

<td colspan="2" align="center"><input type="submit" value="Edit" name="btnEdit"> </td>

</tr>

</table>

</form>

Now we will run our example using the below Url in the browser.

http://localhost/codeigniter4_crud/users

It will help you...

#Codeigniter