In this tutorial, We will explain to you how to create codeigniter crud operations with mysql. so we will create a simple CRUD application in CodeIgniter with MySQL to perform create (insert), read (select), update, and delete operations.
CRUD stands for create, read, update and delete. if you create a user-friendly website at that time need to crud operation.
if you want to create CRUD operation in CodeIgniter, then the first configuration of the database and connect the MySQL database then after creating CRUD operation in Codeigniter.
in our post through, We will go how to connect CodeIgniter to MySQL and perform the CRUD operation.
CREATE TABLE IF NOT EXISTS `register` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(64) NOT NULL, `last_name` varchar(64) NOT NULL, `address` text NOT NULL, `email` varchar(64) NOT NULL, `mobile` varchar(12) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
Go to config folder and open database.php file some changes in this file like as hostname, database username, database password and database name.
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'codeigniter_pagination', '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 );
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('register_model');
}
public function index()
{
$arrData['register_detail'] = $this->register_model->get_all_register_detail();
$this->load->view('register/list',$arrData);
}
public function add()
{
if($this->input->post('btnadd'))
{
$arrData['first_name'] = $this->input->post('txtFname');
$arrData['last_name'] = $this->input->post('txtLname');
$arrData['address'] = $this->input->post('txtAddress');
$arrData['email'] = $this->input->post('txtEmail');
$arrData['mobile'] = $this->input->post('txtMobile');
$insert= $this->register_model->insert($arrData);
if($insert)
{
redirect('register');
}
}
$this->load->view('register/add');
}
public function edit($id)
{
$arrData['register_detail'] = $this->register_model->get_id_wise_register_detail($id);
if($this->input->post('btnEdit'))
{
$editData['first_name'] = $this->input->post('txtFname');
$editData['last_name'] = $this->input->post('txtLname');
$editData['address'] = $this->input->post('txtAddress');
$editData['email'] = $this->input->post('txtEmail');
$editData['mobile'] = $this->input->post('txtMobile');
$update= $this->register_model->update($editData,$id);
if($update)
{
redirect('register');
}
}
$this->load->view('register/edit',$arrData);
}
public function delete($id)
{
$delete=$this->register_model->delete($id);
if($delete)
{
redirect('register');
}
}
}
?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register_model extends CI_Model
{
public function __construct()
{
$this->load->database('default');
$this->load->library('session');
// Call the Model constructor
parent::__construct();
}
public function get_all_register_detail()
{
$this->db->select('*');
$this->db->from('register');
$objQuery = $this->db->get();
return $objQuery->result_array();
}
public function get_id_wise_register_detail($id)
{
$this->db->select('*');
$this->db->from('register');
$this->db->where('id',$id);
$objQuery = $this->db->get();
return $objQuery->result_array();
}
public function insert($arrData) {
if ($this->db->insert('register', $arrData)) {
return true;
} else {
return false;
}
}
public function update($editData,$id)
{
$this->db->where('id', $id);
if ($this->db->update('register', $editData)) {
return true;
} else {
return false;
}
}
function delete($id)
{
if($this->db->delete('register', array('id' => $id)))
{
return true;
}
else
{
return false;
}
}
}
?>
<html>
<head>
<title>codeigniter crud operation with mysql</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script
</head>
<body>
<table border="1" align="center">
<tr>
<td colspan="5" align="right"><a href="<?php echo base_url(); ?>register/add">Add</a></td>
</tr>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
<td>Mobile</td>
<td>Action</td>
</tr>
<?php
foreach($register_detail as $rg){
?>
<tr>
<td><?php echo $rg['first_name']; ?></td>
<td><?php echo $rg['last_name']; ?></td>
<td><?php echo $rg['email']; ?></td>
<td><?php echo $rg['mobile']; ?></td>
<td><a href="<?php echo base_url(); ?>register/edit/<?php echo $rg['id']; ?>">Edit</a> <a href="<?php echo base_url(); ?>register/delete/<?php echo $rg['id']; ?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
<form method="post" name="frmAdd">
<table align="center">
<tr>
<td colspan="2" align="center">Add Record</td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="txtFname"> </td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="txtLname"> </td>
</tr>
<tr>
<td>Address</td>
<td><textarea name="txtAddress" rows="4" cols="16"></textarea> </td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail"> </td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="txtMobile"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Add" name="btnadd"> </td>
</tr>
</table>
</form>
<form method="post" name="frmEdit"> <table align="center"> <tr> <td colspan="2" align="center">Edit Record</td> </tr> <tr> <td>First Name</td> <td><input type="text" name="txtFname" value="<?php echo $register_detail[0]['first_name']; ?>"> </td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="txtLname" value="<?php echo $register_detail[0]['last_name']; ?>"> </td> </tr> <tr> <td>Address</td> <td><textarea name="txtAddress" rows="4" cols="16"><?php echo $register_detail[0]['address']; ?></textarea> </td> </tr> <tr> <td>Email</td> <td><input type="text" name="txtEmail" value="<?php echo $register_detail[0]['email']; ?>"> </td> </tr> <tr> <td>Mobile</td> <td><input type="text" name="txtMobile" value="<?php echo $register_detail[0]['mobile']; ?>"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Edit" name="btnEdit"> </td> </tr> </table> </form>
We think would you like this article, so you can click on the “Show Demo” button and you can see this demo article.