Insert Data
How to Insert Data into Database.

1- Create Table in Database(phpmyadmin)

2- Create HTML page  - To accept user input

3- Create Controller    - To Run Script

4- Create Model - To Insert Query

HTML page for User input and Save it in View Folder

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html>
<head>
<title>Insert Data</title>
</head>
<body>
<form name="frm" action="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>

Controller- That Run Script(Website user interact with controller

<?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->model('user_model');
}

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

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

}

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