Call Now

WhatsApp

Go back
to top

Arrow Icon
Site Logo

PHP CodeIgniter - Basic CRUD operation with MySQL Database with example

Shape Image Layout Star Icon Star Icon

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';
To
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` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(255) NOT NULL,
 `description` text NOT NULL,
 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `updated_at` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

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.

posts.php
create-post.php
edit-post.php
posts.php

 



 
 
 
 Codeigniter 4 CRUD Tutorial -  posts List Example - Blazingcoders
 
 

 

 

 


 

 


    Create
   
 

     
       
         
             
             
             
             
         
       
       
         
         
         
             
             
             
             
         
         
         
       
     

Id Title Description Action

              Edit
              Delete
             


 


 

 

 

 

 

 

Tags :

Codeigniter crud operations with MySQL download

Codeigniter crud operations with MySQL GitHub

Codeigniter 4 crud operations with MySQL

Codeigniter crud bootstrap
crud operations with file upload in Codeigniter

crud operation in Codeigniter GitHub

crud operation in Codeigniter using ajax

Codeigniter crud generator

Related Posts

CodeIgniter Basic CRUD operation with MySQL Database with an example, CodeIgniter CRUD Function

Business Directory and Listing Development

Blazingcoders provides directory and listing website development services. A Business Directory webs

Read More
CodeIgniter Basic CRUD operation with MySQL Database with an example, CodeIgniter CRUD Function

Why Every Travel Agency Needs a Custom Website Design

Everyone enjoys travel and travel becomes a hobby to the peoples over all the world. Travelers are i

Read More

Security Testing Company India

The major concerns of an application developer and the client today are software security. Security

Read More