Insert using codeigniter
1)Download codeigniter from codeigniter.com.
2)Connect a database by going into application/config/database.php file.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost', // put your phymyadmin hostname
'username' => 'root', // put your phymyadmin username
'password' => '', // put your phymyadmin password
'database' => 'interview', // this is database name
'dbdriver' => 'mysqli');
3)Set base_url in application/config/config.php
e.g:$config['base_url'] = 'http://localhost:8080/codeigniter/index.php';
and put a url helper in application/config/autoload.php file.
4)For reduce the code we have to use autoload.php file located at application/config/autoload.php
in this file we should autoload a database and model.
e.g.:$autoload['libraries'] = array('database');
$autoload['model'] = array('Model');
5)Extensions of all files should be ".php".
6)Create a page in application/views .
7)Create a new controller in application/controllers .
8)Create a model in application/models .
<head>
</head>
<body>
<form name="frm" id="frm" method="post" action=<?php echo base_url()?>Cnt/insert_data>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="emp_name" id="emp_name"></td>
</tr>
<tr>
<td>Salary</td>
<td><input type="number" name="emp_salary" id="emp_salary"></td>
</tr>
<tr>
<td><input type="submit" name="btn" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
defined('BASEPATH') OR exit('No direct script access allowed');
class Cnt extends CI_Controller {
public function insert_view()
{
$this->load->view('insert');
}
public function insert_data(){
$name=$this->input->post('emp_name');
$salary=$this->input->post('emp_salary');
$arr=array(
'emp_name'=>$name,
'emp_salary'=>$salary
);
$resp=$this->Model->insert_data('emp1',$arr);
echo "<script>alert('$resp')</script>";
$this->insert_view();
}
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model extends CI_Model {
public function insert_data($tbl,$arr)
{
$this->db->insert($tbl,$arr);
}
}
Display using codeigniter
2)Connect a database by going into application/config/database.php file.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost', // put your phymyadmin hostname
'username' => 'root', // put your phymyadmin username
'password' => '', // put your phymyadmin password
'database' => 'interview', // this is database name
'dbdriver' => 'mysqli');
3)Set base_url in application/config/config.php
e.g:$config['base_url'] = 'http://localhost:8080/codeigniter/index.php';
and put a url helper in application/config/autoload.php file.
4)For reduce the code we have to use autoload.php file located at application/config/autoload.php
in this file we should autoload a database and model.
e.g.:$autoload['libraries'] = array('database');
$autoload['model'] = array('Model');
5)Extensions of all files should be ".php".
6)Create a page in application/views .
7)Create a new controller in application/controllers .
8)Create a model in application/models .
→code of insert.php file located at application/views :-
<html><head>
</head>
<body>
<form name="frm" id="frm" method="post" action=<?php echo base_url()?>Cnt/insert_data>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="emp_name" id="emp_name"></td>
</tr>
<tr>
<td>Salary</td>
<td><input type="number" name="emp_salary" id="emp_salary"></td>
</tr>
<tr>
<td><input type="submit" name="btn" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
Controller code for insertion :-
<?phpdefined('BASEPATH') OR exit('No direct script access allowed');
class Cnt extends CI_Controller {
public function insert_view()
{
$this->load->view('insert');
}
public function insert_data(){
$name=$this->input->post('emp_name');
$salary=$this->input->post('emp_salary');
$arr=array(
'emp_name'=>$name,
'emp_salary'=>$salary
);
$resp=$this->Model->insert_data('emp1',$arr);
echo "<script>alert('$resp')</script>";
$this->insert_view();
}
}
Model code for Insertion :-
<?phpdefined('BASEPATH') OR exit('No direct script access allowed');
class Model extends CI_Model {
public function insert_data($tbl,$arr)
{
$this->db->insert($tbl,$arr);
}
}
Comments
Post a Comment