Codeigniter 4 CRUD (Create Read Update Delete) Tutorial For Beginners – onlinecode

Codeigniter 4 CRUD (Create Read Update Delete) Tutorial For Beginners – onlinecode

In this post we will give you information about Codeigniter 4 CRUD (Create Read Update Delete) Tutorial For Beginners – onlinecode. Hear we will give you detail about Codeigniter 4 CRUD (Create Read Update Delete) Tutorial For Beginners – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, We will inform you how to perform crud operation in CodeIgniter 4. Codeigniter 4 CRUD (Create Read Update Delete) Tutorial For Beginners.

if you want to create CRUD operation in CodeIgniter 4, so you can follow the below steps.OverviewStep 1: Download CodeigniterStep 2: Basic ConfigurationsStep 3: Create a Database in tableStep 4: Connect to DatabaseStep 5: Create Controller and ModelStep 6: Create Views FilesStep 7: Run The Application

Step 1: Download CodeigniterIf you want to download or install CodeIgniter 4 then you can below Url.How To Install Codeigniter 4 Using Manual, Composer, Git

Step 2: Basic ConfigurationsIf you want to Basic Configurations in your project then you can below Url.Codeigniter 4 Removing Index.Php From Url

Step 3: Create a Database in tableIn this step, We will create the database and table.

CREATE TABLE IF NOT EXISTS 'students' (
  '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 ;

Step 4: Connect to DatabaseGo to “app/Config/Database.php” folder and open database.php file some changes in this file like as hostname, database username, database password and database name.

	public $default = [
		'DSN'      => '',
		'hostname' => 'localhost',
		'username' => 'root',
		'password' => '',
		'database' => 'codeigniter4_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,
	];

Step 5: Create Controller and ModelIn this step, we will a create “Student.php” controller and “StudentModel.php” model.app/Controllers/Student.php

<?php namespace AppControllers;
 
use CodeIgniterController;
use AppModelsStudentModel;
 
class Student extends Controller
{
 
    public function index()
    {    
        $model = new StudentModel();
 
        $data['students_detail'] = $model->orderBy('id', 'DESC')->findAll();
        
        return view('list', $data);
    }    
 
    public function create()
    {    
        return view('add');
    }
 
    public function store()
    {  
 
        helper(['form', 'url']);
         
        $model = new StudentModel();
		
        $data = [
            'first_name' => $this->request->getVar('txtFname'),
            'last_name'  => $this->request->getVar('txtLname'),
            'address'  => $this->request->getVar('txtAddress'),
            'email'  => $this->request->getVar('txtEmail'),
            'mobile'  => $this->request->getVar('txtMobile'),
            ];
        $save = $model->insert($data);
		return redirect()->to( base_url('student') );
    }
 
    public function edit($id = null)
    {
      
     $model = new StudentModel();
 
     $data['student'] = $model->where('id', $id)->first();
 
     return view('edit', $data);
    }
 
    public function update()
    {  
 
		helper(['form', 'url']);
		 
		$model = new StudentModel();

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

		 $data = [
			'first_name' => $this->request->getVar('txtFname'),
			'last_name'  => $this->request->getVar('txtLname'),
			'address'  => $this->request->getVar('txtAddress'),
			'email'  => $this->request->getVar('txtEmail'),
			'mobile'  => $this->request->getVar('txtMobile'),
			];

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

		return redirect()->to( base_url('student') );
    }
 
    public function delete($id = null){
		$model = new StudentModel();
		$data['user'] = $model->where('id', $id)->delete();
		return redirect()->to( base_url('student') );
    }
}

?>

app/Models/StudentModel.php

<?php namespace AppModels;
use CodeIgniterDatabaseConnectionInterface;
use CodeIgniterModel;
 
class StudentModel extends Model
{
    protected $table = 'Students';
 
    protected $allowedFields = ['first_name','last_name','address','email', 'mobile'];
}
?>

Step 6: Create Views FilesFinally, we will create the list.php, add.php, and edit.php in the app/views directory.app/views/list.php

<html>
<head>
	<title>Codeigniter 4 CRUD (Create Read Update Delete) Tutorial For Beginners - onlinecode</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('student/create') ?>">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($students_detail as $sd){
		?>
		<tr>
			<td><?php echo $sd['first_name']; ?></td>
			<td><?php echo $sd['last_name']; ?></td>
			<td><?php echo $sd['email']; ?></td>
			<td><?php echo $sd['mobile']; ?></td>
			<td><a  href="<?php echo base_url(); ?>/student/edit/<?php echo $sd['id']; ?>">Edit</a>&nbsp;<a  href="<?php echo base_url(); ?>/student/delete/<?php echo $sd['id']; ?>">Delete</a></td>
		</tr>
		<?php
	}
	?>
 </table>
</body>
</html> 

app/views/add.php

<form method="post" name="frmAdd" action="<?php echo site_url('student/store');?>">
        <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><pre name="txtAddress" rows="4" cols="16"></pre> </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>

app/views/edit.php

<form method="post" name="frmEdit" action="<?php echo base_url('student/update');?>">
	<table align="center">
		<tr>
			<td colspan="2" align="center">Edit Record</td>
		</tr>
		<input type="hidden" name="id"  id="id" value="<?php echo $student['id'] ?>">
		<tr>
			<td>First Name</td>
			<td><input type="text" name="txtFname" value="<?php echo $student['first_name']; ?>"> </td>
		</tr>
		<tr>
			<td>Last Name</td>
			<td><input type="text" name="txtLname" value="<?php echo $student['last_name']; ?>"> </td>
		</tr>
		<tr>
			<td>Address</td>
			<td><pre name="txtAddress" rows="4" cols="16"><?php echo $student['address']; ?></pre> </td>
		</tr>
		<tr>
			<td>Email</td>
			<td><input type="text" name="txtEmail" value="<?php echo $student['email']; ?>"> </td>
		</tr>
		<tr>
			<td>Mobile</td>
			<td><input type="text" name="txtMobile" value="<?php echo $student['mobile']; ?>"> </td>
		</tr>
		<tr>
			<td colspan="2" align="center"><input type="submit" value="Edit" name="btnEdit"> </td>
		</tr>
	</table>
</form>

Step 7: Run The ApplicationNow we will run our example using the below Url in the browser.

http://localhost/codeigniter4_crud/student

We think would you like this article, so you can click on the “Download” button and you can download this demo article.

Download

Please follow and like us:

Hope this code and post will helped you for implement Codeigniter 4 CRUD (Create Read Update Delete) Tutorial For Beginners – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

1  +  8  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US