Inserting Data to Database using CodeIgniter

Inserting Data to Database using CodeIgniter

0 1324

  • To insert data into the database, we have to be connected with the database. In the previous blog, we have learned how to make a connection with the database.

Related Topic:

Connecting Database
Codeigniter Interview Questions

Inserting Data into Database:

There are several steps to insert data into the database.

Step 1 Create a table into the database in which you want to insert data.

Step 2 Create a view file insert_view.php in the application/views directory.


<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>

<div class="container">
<h2>Insert Data Form</h2>
<form method="post" enctype="multipart/formdata">
<div class="form-group">
<label for="email">Name:</label>
<input type="text" class="form-control" name="name" placeholder="Enter Name">
<div class="form-group">
<label for="pwd">Roll Number:</label>
<input type="text" class="form-control" name="roll_number" placeholder="Enter Roll Number">
<div class="form-group">
<label for="pwd">Class:</label>
<input type="text" class="form-control" name="class" placeholder="Enter Class">

<input type="submit" name="insert" class="btn btn-success" value="Submit"/>


Step 3 Create a model file Crud_model.php in the application/models directory.


class Crud_model extends CI_Model
function insert_data($name,$roll_number,$class)
$query="insert into `student` values('','$name','$roll_number','$class')";

Step 4 Create a controller file Crud_controller.php in the application/controllers directory.


defined('BASEPATH') OR exit('No direct script access allowed') //for secuirty
public function insert_fun()

//load database

//load Model

//load view

// after submission code


echo "Data inserted Successfully";

Related Topics:

Views in CodeIgniter
Models in Codeigniter
Helpers in Codeigniter

Step 5 Now, go to the browser and enter the following URL to insert data.


Now, you find a form. Fill the form and submit it to get a result.

Best WordPress Hosting


Leave a Reply

    Waiting for your comments