CodeIgniter Insert Data into Database - phptpoint

CodeIgniter Insert Data into Database

/ins>

CodeIgniter Insert Data into Database

In this tutorial, 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.
This is users table structure

Table Name : users
user_id int primary key auto_increment
name char(50)
email varchar(100)
mobile bigint

 

Connecting to a Database

In CodeIgniter, We can connect with database in 2 ways.

    • Automatic Connecting – Automatic Connection can be done using application/config/autoload.php

 

 

  • Manual Connecting – If u want database connection for some specific Controller then u can use Manual Connection.
    in manual Connection u will have to Create connection for every Controller So it would be better to use Automatic Connection.

 

Creating a Controller

Create a new file under the path Application/controllers/Hello.php
Copy the below given code in your controllers.

 

Creating a View

Create a new file under the path Application/views/registration.php
Copy the below given code in your view.

 

Creating a Model

Create a new file under the path Application/models/Hello_Model.php
Copy the below given code in your model.

Calling/run Hello Controller’s savedata method

Open your web browser and Pass : http://localhost/CodeIgniter/index.php/Hello/savedata

Output : View Page
insert database in codeigniter
Output : Database