CodeIgniter Database Insertion
We have successfully transferred our user posting data into the Model through the Controller section. Now we have to insert this data into the database.
I think you have already idea about plane PHP database operations. Like in plane PHP, MySQL is also used here, which is simple and free.

Here you must create a MySQL database.
- Here I create a database using phpmyadmin tool with a name of ‘userdata’.
- In database ‘userdata’, there is only one table named ‘table_user’. You can give any name. And the table contains three columns.
- First one is named with ‘user_id’, which is the primary key and has a property of auto number. So we won’t need to insert the data into this column.
- Second column is named with ‘user_name’ with a datatype of ‘varchar’, which can save our inserting data ‘user name’ and third column is named with ‘user_age’ with a data type of ‘number’ to store our inserting data ‘user age’.

Now we successfully created our PHP MySQL database ‘userdata’.
Now come back to the CodeIgniter.
Here first of all, you need to specify the database name in the CodeIgniter configuration.
- Open CodeIgniter folder, and come to the ‘config’ folder. There you can see a file named ‘database.php’. Open it.
- Here you have to specify all database details such as database name, host, username, password, etc.
Here as a studying purpose, we use ‘localhost’ as our ‘hostname’ which is default there.
- We have to give username as ‘root’ and database name as ‘userdata’ (That is the name of the database that I have created).

Now save the file, you can try ‘Ctrl’ + ‘S’.
Now open your Model class.
Here I select the first method of sending data from Controller into the Model.
- Here we have to two variables named ‘USERAGE and ‘USERAGE’ to insert into the database.
- In previous lesson, we learnt that, we should call a model function to perform a database operation. We have already created a function named ‘addvalue’ in our previous lesson.
Now we are going to insert our variables ‘USERNAME’ and USERAGE’ into the database. There two methods of coding.
Method 1:
CodeIgniter Way of database Insertion
This is the common and perfect method for inserting data in CodeIgniter.
- Type these lines of codes inside your Model function ‘addvalue’.
$this->load->database(); $data = array( 'user_name' => $this->USERNAME, 'user_age' => $this->USERAGE ); $this->db->insert('table_user', $data);
Explanation:
- $this->load->database();This line of code is compulsory which load database into our project class.
- $data = array(
‘user_name’ => $this->USERNAME,
‘user_age’ => $this->USERAGE
);
Here we create an array that contains all the column name on database and their values that to be inserted to the database.
Where user_name is the database column name (that should be inside single quotes), and $this->USERNAME is the value that we have to insert to the column ‘user_name’. There should be a symbol => between column name and its value.

Similarly ‘user_age‘ and $this->USERAGE are the database column and its value respectively.
- $this->db->insert(‘table_user’, $data);The syntax $this->db->insert() is used to insert data into the database.
There are two parameters, first one ‘table_user’ is the table name on our database and $data is the array of values that contains database columns’ name and their values that we created in upper line.
Now total Controller Code:
<?php class User extends CI_Controller { public function register() { $this->load->view("user/registration"); } public function userregister() { $name = $this->input->post("username"); $place = $this->input->post("userplace"); $age = $this->input->post("userage");
$this->load->Model(‘ProjectModel’);
$this->ProjectModel->USERNAME = $name;
$this->ProjectModel->USERAGE = $age;
$this->ProjectModel->addvalue();
}
}
Model Codes:
<?php class ProjectModel extends CI_Model { var $USERNAME = ''; var $USERAGE = 0; public function addvalue() { $this->load->database(); $data = array( 'user_name' => $this->USERNAME, 'user_age' => $this->USERAGE ); $this->db->insert('table_user', $data); } }
Alternative Method:
Traditional Database Insertion
public function addvalue()
{ $this->load->database(); $this->db->query("INSERT INTO table_user (user_name,user_age) VALUES ('$this->USERNAME',$this->USERAGE)"); }
Explanation:
- $this->load->database();
This line of code is compulsory that we learnt in first method.
- $this->db->query(“INSERT INTO table_user (user_name,user_age) VALUES (‘$this->USERNAME’,$this->USERAGE)”);
This line is the tradition method of database insertion.
You can insert data by insert traditional insert query. That is, $this->db->query(“insert query”) is enough to perform insert operation.
Where
- $this->db->query(“”) is the default statement to insert data into the database in CodeIgniter.
- table_user is the database table that we want to insert our data.
- user_name and user_age are the columns on the database.
- $this->USERNAME and $this->USERAGE are the values that are to be inserted into the database. Which are posted from the user view section. Variable $this->USERNAME should be inside quotes because its value will be a string and number variable should not be inside any quotes, that is why $this->USERAGE does not contains quotes.
Now check your database, there should be values.
Sample output:
View:

Database:

Like this, we can perform update, delete, select operations with our database. We will learn them in coming lessons.
Be First to Comment