Introduction
In this tutorial, I will show you step-by-step how to insert data into a database & how to display database records using CodeIgniter.
Why use CodeIgniter?
For web application development, CodeIgniter is one of the best frameworks. CodeIgniter development is fast and reliable compared to other frameworks. For connecting to the database, CodeIgniter provides out of the box libraries and performs various operations like sending an email, uploading the file, etc...
Feature of CodeIgniter Framework
- CodeIgniter is based on the Model-View-Controller system
- CodeIgniter is a lightweight system
- Form and Data Validation
- CodeIgniter is provide Sending email class, Attachment, HTML/TEXT email, multiple protocols, and more
- Data Encryption
- Benchmarking
- Localization
- Pagination
Let’s start the insertion steps.
Step 1
First, you need to create a table for insert data into the table. Use the below code to create a table. (Give your table name as per your requirement). The below code indicates that if the table already exists in the database, then remove/drop those tables and create a new table with the following fields.
- DROP TABLE IF EXISTS `tb_category`;
- CREATE TABLE IF NOT EXISTS `tb_category` (
- `ct_id` int(3) NOT NULL AUTO_INCREMENT,
- `ct_name` varchar(20) NOT NULL,
- `ct_descritpion` text NOT NULL,
- `cr_date` date NOT NULL,
- PRIMARY KEY (`ct_id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;
Step 2
In the 2nd step, you need to create a view file. Open the application/views/ and create a new view file category_form.php (Your View File Name). The below code is used for the user interface. It's the HTML file. You can also use bootstrap and CSS to create an attractive form. In form tag action=”your_controller_name/insert_function”.
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <table>
- <form action="My_controller/insert_category" method="post">
- <tr>
- <td>Enter Category Name :</td>
- <td>
- <input type="text" name="category" id="category">
- </td>
- </tr>
- <tr>
- <td>Enter Description :</td>
- <td>
- <textarea name="descripiton" id="descripiton"></textarea>
- </td>
- </tr>
- <tr>
- <td>
- <input type="submit" value="Submit">
- </td>
- </tr>
- </form>
- </table>
- </body>
- </html>
Step 3
In the 3rd step, you need to create a controller file in the application/controllers/ directory and create a new controller called My_controller.php (Your controller name). Create methods in the controller class to perform CRUD operation.
The below code is used to retrieve data on the controller from the form using the post method. After that, all data are stored in an array and the array stored in one variable. After that, all those data are sent on the model file.
“$insert_category = $this->Category_model->insert_category($data);” the meaning of this line / code is used for send all data on model file.
Header() is used for redirect on the display page or any other page. base_url is your route URL you have to define base_url in your application/config/ directory open config.php file put your project URL.
$config['base_url'] = 'http://localhost/InterView_Test/Practical_Task_3/';.
- <?php
- /**
- *
- */
- class My_controller extends CI_Controller{
- function __construct()
- {
- parent::__construct();
- $this->load->helper('url'); /* This helper is used to load your URL */
- $this->load->model("Category_model"); /* This used to load model */
- }
- public function index(){
- $this->load->view('category_form.php');
- }
- public function insert_category(){
- $cr_date = date("Y/m/d");
- $data = array(
- 'ct_name' => $this->input->post('category'),
- 'ct_descritpion' => $this->input->post('descripiton'),
- 'cr_date' => $cr_date,
- );
- $insert_category = $this->Category_model->insert_category($data);
- header("location:".base_url()."index.php/My_controller/display");
- /* Note: base_url is your route URL you have to define base_url in your application/config/ directory open config.php file. Put your project URL
- $config['base_url'] = 'http://localhost/InterView_Test/Practical_Task_3/'; */
- }
- }
- ?>

Roshan RathodPosted Sep 18, 2020, 7:18 AM
Good one ..keep it up
Nikhil DudharejiaPosted Sep 13, 2020, 1:02 AM
Good going