Simple Mysql CRUD Operation in CodeIgniter with Example

CodeIgniter is one of most popular PHP framework. CRUD is one of the primary functions of a Database Management System. CRUD stands for "Create, Read, Update and Delete".  Creating CRUD grid is a very common thing of 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 download codeigniter and paste CI project in your XAMPP server folder. Then, browse http://localhost/demo url for checking welcome screen as like as bellows picture.

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

Step: 2

In 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 base URL for our project. Set up base URL look like bellows example.

http://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 database & table.

Step: 3

 You must create 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 database using by http://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 create a table “entries_new”. Now we are going to configure our database to our CI project.

Step: 3

Just open database.php file from “\demo\application\config\database.php”. Find database name, user name, host name, 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 controller. You can get this in “demo/application /controllers/file.php”.  Here is a controller contain 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 create controller, we need to create model. Model is for database operation.

Step: 5

Given bellow a example model. This model is for data operation from database.  Follow bellows code to create 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 web browser. I have use bellows code for 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 understanding of CRUD process in codeigniter framework.  Above tutorial is to show install CRUD to your CI project. It’s easy to configure and use CRUD.

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at discussdesk@gmail.com

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments