How to insert data in codeigniter

For creating table the SQL query is:

  `id` int(11) NOT NULL  AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `email` varchar(30) NOT NULL,
  `password` varchar(30) NOT NULL,

Create a view file insert.php and save the below code it in application/views/insert.php.

<!DOCTYPE html>
<title>Insertdata form</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>

    <div class="container">
        <div class="row">
            <div class="col-lg-6">
                <form action="<?= base_url('demo/save') ?>" method="post">
                    <input type="text" name="name" class="form-control" placeholder="Enter your name">
                    <input type="email" name="email" class="form-control" placeholder="Enter your email">
                    <input type="text" name="password" class="form-control" placeholder="Enter your password">
                    <input type="submit" value="Insertdata" class="form-control">

Create a controller file Demo.php and save it in application/controller/Demo.php.

defined('BASEPATH') OR exit('No direct script access allowed');

class Demo extends CI_Controller {
    public function __construct() {
    function insert(){
    function save(){
        $data = $this->input->post();
        $result = $this->Demo_Model->save("demo",$data);
        if (!empty($result)){

Create a model file Demo.php and save it in application/models/Demo_Model.php.


class Demo_Model extends CI_Model{
    function save($table_name,$data){
        return $this->db->insert_id();

Let us execute this example by the following URL in the browser. This URL may be different based on your website.