Insert Data Into Mysql In Codeigniter

In this post we will learn , ho we can insert data into databse in codeigniter.

Step 1 : Create Table:

CREATE TABLE IF NOT EXISTS `emp1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
`mobno` bigint(10) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) 
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Step 2 :Application/config/database.php :

defined('BASEPATH') OR exit('No direct script access allowed');
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => 'root',
	'database' => 'test',
	'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 Controller (CRUD.php):

class Crud  extends CI_Controller {

	public function __construct(){
        parent::__construct();
         
           $this->load->model('CRUDModel');
    }
    public function index(){             
	$this->load->view('create');	
     }

    public function store(){
       
         $retval = $this->CRUDModel->save($this->input->post());
         $this->index();
    }
}

Step 4 : Create Model(CRUDModel.php):

defined('BASEPATH') OR exit('No direct script access allowed');
class CrudModel extends CI_Model{

    private $table = 'emp';

    public function save($data){
	 
       unset($data['submit']);
       $this->db->insert($this->table,$data)
       return true;		 
   }

}

Step 5 : Create View(create.php):

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Ci Crud Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>  
</head>
<body>
<h1 class="text-success" align="center">CI Insert Example</h1>
<div class="container">    
<form class="form-horizontal" method="post" action="<?php echo site_url();?>/crud/store">
            <div class="form-group">
            <label class="control-label col-sm-2" for="email">Name:</label>
            <div class="col-sm-10">
                <input type="text" class="form-control"  placeholder="Enter Name" name="name">
            </div>
            </div>            
            <div class="form-group">
            <label class="control-label col-sm-2" for="email">Email:</label>
            <div class="col-sm-10">
                <input type="email" class="form-control"  placeholder="Enter email" name="email">
            </div>
            </div>
            <div class="form-group">
            <label class="control-label col-sm-2" for="pwd">Mobno:</label>
            <div class="col-sm-10">          
                <input type="number" class="form-control"  placeholder="Enter Mobile No" name="mobno">
            </div>
            </div>
             
            <div class="form-group">        
            <div class="col-sm-offset-2 col-sm-10">
                <input  type="submit" class="btn btn-success" name="submit" value="Submit">
            </div>
            </div>
  </form>

 </div>
</body>
</html>

Exit mobile version