Simple Mysql CRUD Operation in CodeIgniter with Example

Abhigyan Singh 09th Oct 2020

CodeIgniter is one of the most popular PHP frameworks. CRUD is one of the primary functions of a Database Management System. CRUD stands for "Create, Read, Update and Delete".  Creating a CRUD grid is a very common thing today. In SQL, it represents INSERT, SELECT, UPDATE, and DELETE commands.

In that example, we saw the basics of how CodeIgniter works and the basics of the MVC pattern. Now I am going to demonstrate the basic installation of CodeIgniter, a PHP Framework, and a simple example of building a CRUD with it. Just follow bellows steps for CRUD.

(Also Read: Bootstrap Datatable With Add Edit Remove option In Php Mysql and Ajax)

Step: 1

Firstly I have download start configuring Codeigniter. You also need to download Codeigniter and paste the CI project in your XAMPP server folder. Then, browse https://localhost/demo URL for checking the welcome screen as below as bellows picture.

If you got like the above screen, you can go to the next step. The next step of my work process is configuring settings for the database.

Step: 2

In the Configurations section, we will configure settings for the database to help build this CRUD sample.  Firstly need to configuration our config.php file from “\demo\application\config\config.php”.

Then we’ll set up a base URL for our project. Set up base URL looks like bellows example.

https://localhost/demo.

Replace "demo" by your CI folder name from your XAMPP server” (if you want to change folder name). After configuration, we need to create a database & table.

Step: 3

You must create a database and table for SQL operation. Now I’m creating a database and table in using SQL query with PHP. Create a database from PHPMyAdmin. Copy-paste bellows code and create a database using https://localhst/phpmyadmin.

CREATE TABLE IF NOT EXISTS ` entries_new` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`mobile` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

We’ve created a table “entries_new”. Now we are going to configure our database to our CI project.

Step: 3

Just open the database.php file from “\demo\application\config\database.php”. Find database name, user name, hostname, database user name, password, etc. Now we’ll edit that information according to our created database. Look like bellows code.

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['default']['failover'] = array();

Step: 4

In this step, we need to create a controller. You can get this in “demo/application /controllers/file.php”.  Here is a controller contain a class name (File).

class File extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model("user");

}
public function index()
{
$data['ResAre']=$this->user->loginvalidate();
if($data['ResAre'])
{
redirect( base_url().'index.php/manage_page');
}
else
{
redirect( base_url().'index.php/login_page');
}
}
}

After creating the controller, we need to create a model. Model is for database operation.

Step: 5

Given bellow an example model. This model is for data operation from the database.  Follow bellows code to create a model.

class user extends CI_Model
{

function loginvalidate()
{

$email=$this->input->post('email');
$pwd=$this->input->post('password');

$this->db->select('*');
$this->db->from('entries_new');
$this->db->where('email',$email);
$this->db->where('password',$pwd);
$query=$this->db->get();

if($query->num_rows==1)
{
$result=$query->result();
$se_data=array('username'=>$result[0]->name,
'email'=>$result[0]->email);
$this->session->set_userdata($se_data);
return $result;
}
else
{ return false; }
}
}

Step: 6

Finally, need a view page for display application UI by the web browser. I have use bellows code for the view page.

<form method="POST" action="<?php echo base_url();?>index.php/login/loginform">
<table border="1" align="center">
<caption>Login Form</caption>
<tr><td>Email</td><td><input type="email" name="email" required/></td></tr><br/>
<tr><td>Password</td><td><input type="password" name="password" required/></td></tr><br/>
<tr align='center'><td colspan="2"><input type="submit" name="login" value="LOG IN"/></td></tr>
<td colspan="2"><div align="center"><a href="<?php echo base_url();?>index.php/admin/admin/index">New User</a></div></td>
</table>
</form>

</html>

Hope now you have an understanding of the CRUD process in the Codeigniter framework. The above tutorial is to show install CRUD to your CI project. It’s easy to configure and use CRUD.

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk