• 18 Jan, 2025

How to Insert Data in Database using Codeigniter

How to Insert Data in Database using Codeigniter

How to Insert Data in Database using Codeigniter

In this tutorial, we have handled Codeigniter (insert Update delete ) curd opration using mysql,jQuery. Insert Data  With html form in Codeigniter,  The Html form submit and store into MySQL database

We will understand how to insert data into database using Controller model and view. We will use users table to insert, display, update and delete.

Before start coding let's understand why we use codeigniter and Feature of CodeIgniter Framework

Why use CodeIgniter?

CodeIgniter is one of the greatest frameworks for developing web apps. Compared with other frameworks, CodeIgniter programming is dependable and quick. CodeIgniter offers pre-built libraries for database connections and handles a number of tasks, such as emailing, uploading files, and more.

Feature of CodeIgniter Framework

  •  CodeIgniter is based on the Model-View-Controller (MVC) system
  • CodeIgniter is a lightweight system
  • Open-source
  • Simple interface
  • CodeIgniter is provide Sending email class, Attachment, HTML/TEXT email, multiple protocols, and more
  • Data Encryption
  • Security
  • Localization
  • Pagination
  • Error logging
  • Search-engine friendly URLs
  • Flexible URI routing
        

    We have handled tutorial in very easy steps. So let’s start the coding

Here put some sample code. for full source code click download now end of post


Step 1: Create a MySQL database table .  

 CREATE TABLE IF NOT EXISTS `codeignitercrud` (
 `id` int NOT NULL AUTO_INCREMENT,
 `email` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
 `firstname` varchar(250) COLLATE utf8mb4_general_ci NOT NULL,
 `lastname` varchar(250) COLLATE utf8mb4_general_ci NOT NULL,
 `address` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
 `gender` varchar(250) COLLATE utf8mb4_general_ci NOT NULL,
 `hobbies` varchar(250) COLLATE utf8mb4_general_ci NOT NULL,
 `img` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
 `phonenumber` bigint NOT NULL,
 `DOB` date NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

Step 2: Download CodeIgniter & connection to the MySQL database.  


Download codeigniter from his official website
https://www.codeigniter.com/download

Database connection application/config/database.php

Sample code

 $db['default'] = array(
    'dsn'    => '',
    'hostname' => 'localhost',
    'username' => 'Database_username',
    'password' => 'Database_password',
    'database' => 'Database_name',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);


Step 3:  Create the HTML for the Register Form .

In html form we can use textbox, radio button, checkbox, textarea , image

Registatin form application/views/registation.php

Screen short 

How to Insert-update-delete Data in Database using Codeigniter

Step 4: Use codeigniter ,jQuery to validate the form and submit it.  

Validation form application/controllers/user.php

Set rules for validation

Sample code

 $this->form_validation->set_error_delimiters('<div class="error text-danger py-1">', '</div>');
       $this->form_validation->set_rules('email', 'email', 'required');
       $this->form_validation->set_rules('firstname', 'Firstname', 'required');
       $this->form_validation->set_rules('lastname', 'Lastname', 'required');
       $this->form_validation->set_rules('address', 'Address', 'required');
       $this->form_validation->set_rules('gender', 'Gender', 'required');
       $this->form_validation->set_rules('hobbies[]', 'Hobbies', 'required');
       $this->form_validation->set_rules('phonenumber', 'Phonenumber', 'required|max_length[10]|min_length[10]');
       $this->form_validation->set_rules('DOB', 'DOB', 'required');

Screen short 

How to Insert-update-delete Data in Database using Codeigniter-validation
 

Step 5: submit data Save in to database server.  

Now create function Insert & save data in to database

Sample code

  $formArray = array(
               'email' => $this->input->post('email'),
               'firstname' => $this->input->post('firstname'),
               'lastname' => $this->input->post('lastname'),
               'address' => $this->input->post('address'),
               'gender' => $this->input->post('gender'),
               'hobbies' => implode(',', $this->input->post('hobbies')),
               'img' => $upload_data['file_name'],
               'phonenumber' => $this->input->post('phonenumber'),
               'DOB' => $this->input->post('DOB')
           );
           $this->User_model->create($formArray);


Download Full Code HERE 

Download Fome Here