PHP CodeIgniter - Basic CRUD Operation With MySQL Database With Example

PHP CodeIgniter - Basic CRUD operation with MySQL Database with example

In this Post PHP CodeIgniter 4 instructional exercise, I will tell you the fundamental CRUD usefulness with MySQL Database. 

Muck is an abbreviation for Create, Read, Update, and Delete information base records. 

This is quite possibly the most well-known errand in the improvement to make something in the table, update some data, read information from a table, and erase data. 

In this model, I will utilize the bootstrap library to list posts and render the structures to make and alter posts. 

Download and Configure Codeigniter 4 Application 

In the initial step, I will download the new form of Codeigniter 4, Go to this connection constantly Codeigniter 4, and unfasten this application in your working registry and change the application name to muck. 

Run the writer order to download the conditions.

Now I will configure the base URL in app/Config/App.php.

public $baseURL = 'http://localhost:8080';
public $baseURL = 'http://localhost/crud/public';

Configure Database and Create Table

In this progression, I will make an information base originally called muck and make another table post in this muck data set. 

You can run the underneath SQL inquiry to make posts table in your information base.

CREATE TABLE `posts` (
 `title` varchar(255) NOT NULL,
 `description` text NOT NULL,
 `updated_at` timestamp NULL DEFAULT NULL,

Presently I need to refresh the information base qualification in the data set config document. 

I need to go application/Config/Database.php and open the Database.php document in a proofreader.

public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'crud',
        'DBDriver' => 'MySQLi',
        'DBPrefix' => '',
        'pConnect' => false,
        'DBDebug'  => (ENVIRONMENT !== 'production'),
        'cacheOn'  => false,
        'cacheDir' => '',
        'charset'  => 'utf8',
        'DBCollat' => 'utf8_general_ci',
        'swapPre'  => '',
        'encrypt'  => false,
        'compress' => false,
        'strictOn' => false,
        'failover' => [],
        'port'     => 3306,

Create a Model

In this step, I will create a model file PostModel.php to communicate with the posts table in the app/Models/ directory.

namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
class PostModel extends Model
    protected $table = 'posts';
    protected $allowedFields = ['title', 'description'];


Create Controller

In this step, I will create a controller record Posts. Hypertext Preprocessor in the app/controllers directory. In this controller record, I will create a few approaches to carry out crud operation.

index() - this method is used to list all the posts.
create() - this method is used to render the create form.
store() - this method is used to insert the new post into the table.
edit() - this method is used to render the edit form.
update() - this method is used to update the post information.
delete() - this method is used to delete the post information from a database table.

use CodeIgniter\Controller;
use App\Models\PostModel;
class Posts extends Controller
    public function index()
        $model = new PostModel();
        $data['posts'] = $model->orderBy('id', 'DESC')->findAll();
        return view('posts', $data);
    public function create()
        return view('create-post');
    public function store()
        helper(['form', 'url']);
        $model = new PostModel();
        $data = [
            'title' => $this->request->getVar('title'),
            'description'  => $this->request->getVar('description'),
        $save = $model->insert($data);
        return redirect()->to( base_url('posts') );
    public function edit($id = null)
     $model = new PostModel();
     $data['post'] = $model->find($id);
     return view('edit-post', $data);
    public function update()
        helper(['form', 'url']);
        $model = new PostModel();
        $id = $this->request->getVar('id');
        $data = [
            'title' => $this->request->getVar('title'),
            'description'  => $this->request->getVar('description'),
        $save = $model->update($id,$data);
        return redirect()->to( base_url('posts') );
    public function delete($id = null)
     $model = new PostModel();
     $data['post'] = $model->where('id', $id)->delete();
     return redirect()->to( base_url('posts') );


Create Views

In this step, I will create the respective HTML files.



 Codeigniter 4 CRUD Tutorial -  posts List Example - Blazingcoders








Id Title Description Action









Related Posts
PHP Code Check time difference is between two time slots

We are going to explain how to get the time difference between timeslots. below are the simple examp

Corporate Website Design

A well branded corporate website brings marketing strength to the company. We provide complete custo

Would you like to exchange Guest Posts with us?

Blazingcoders love to exchange articles and Guest posting. We love to share knowledge, learning, and

How to set up gmail to send and receive emails using your domain name

In this blog post, I’ll show you ways to line up Gmail together with your&

Get free quote for

CodeIgniter Basic CRUD operation with MySQL Database with an example

CodeIgniter CRUD Function

Contact us