Insert Data
How to Insert Data into Database.

1- Create Table in Database(phpmyadmin)

2- Create HTML page  - To user input form

3- Create Controller    - To Run Script

4- Create Model - To  write insert query

Create page and save in view folder (View->inputdata.php)

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html>
<head>
<title>Insert Data</title>
</head>
<body>
<form name="frm" action="<?php echo base_url(); ?>index.php/user/insert" method="post">
Enter Name<input name="name"  type="text"  value=""/><br />
Enter Email<input name="email"  type="text" value=""/><br />
Enter City<input name="city"  type="text"  value=""/><br />
<input name="btn"  type="submit"  value="Submit"/>
</form>
</body>
</html>

Create Controller and save in controller folder(View->user.php)

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

class User extends CI_Controller {

public  function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->model('user_model');
}

public  function index()
{
$this->load->view('inputdata');
}

public function insert()
{
$this->user_model->user_insert();
$this->load->view('inputdata');
}

}

Model - To insert query to insert data into database

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

class User_Model extends CI_Model {

public function user_insert()
{

$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'city' => $this->input->post('city')
);

$this->db->insert('tbl_user', $data);
}

}


Previous
Next