Learn PHP,HTML,CSS,Javascript,MySQL,Jquery

Saturday, 18 April 2020

PHP Codeigniter CRUD Basic Tutorial With MySQL Database

CodeIgniter Basic CRUD Operation With MySQL

Codeigniter basic crud tutorial example. Today I will show you step by step how to perform crud operation with codeigniter project using mysql database. In this tutorial you will learn easy inster,read,update and delete operation with codeigniter from scrtach.

CRUD Operation Codeigniter tutorial - PHP CRUD Codeigniter MySQL Database. Basic crud application using codeigniter and mysql. Insert html form data into mysql table and retrieve data feom mysql table and display in html table. Here you will get answer of the question how to display data from mysql table in html table using codeigniter framework. Update mysql table data codeigniter. Delete mysql table data codeigniter. How to insert data into mysql table using codeigniter.

PHP Codeigniter CRUD
Codeigniter CRUD


CRUD operation is the very basic and important functionality in any web application. We all perform crud operation while we do web development.

Codeigniter framework works using oop structure and mvc. We all know in oop we build programs using classes and functions. MVC stands for Model View controller. MVC separtes application into three components. Model represents shape of the data and business logic, view represents data to user interface and controller represents the application logic.

You can read more about MVC Here

Download Code Igniter Framework

To start developing crud in codeigniter you must have to install the setup of codeigniter framework. You can download from this link Download Codeigniter . Codeigniter is a very lightweight framework it will not longer time to download. After downloading extract to your working directory.

Database Configuration

To perform codeigniter crud you must have to create a database and a table in mysql database. So first I will create a test database and a table within database.
Run the following query to create person_detail table in test database.
CREATE TABLE `person_detail` ( `id` int(11) NOT NULL, `firstname` varchar(100) NOT NULL, `lastname` varchar(100) NOT NULL, `email` varchar(150) NOT NULL, `phone` varchar(50) NOT NULL, `address` varchar(255) NOT NULL, `postal_code` varchar(20) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `person_detail` ADD PRIMARY KEY (`id`); ALTER TABLE `person_detail` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Now we have a person_detail table in mysql database to perform crud operation on this table using php codeigniter.

Let's configure the database within codeigniter application folder. Open your working directory folder in which you have codeigniter setup. Next open the application folder and within this folder open the config folder.Within this config folder open database.php file and change the name of database and write your database name.
Codeigniter Change DB Name
Codeigniter-Change Database Name

Base URL Configration

Open the config.php file from config folder. Now change base url variable to your working directory folder name as in this example working directory folder name is practice.
Codeigniter-Base URL
Codeigniter-Base URL

Remove index.php From URL

Now I will write code to remove index.php from url and it is the answer of how to remove index.php from codeigniter url. First of all open notepad or notepad++ or any other editor you are using. Add the following code and click on ctrl+s to save and file name will be .htaccess.

<ifmodule authz_core_module=""> Require all denied </ifmodule> <ifmodule authz_core_module=""> Deny from all </ifmodule>
Now we all set to build codeigniter crud application. First of all lets talk about codeigniter crud model functionality.

Codeigniter CRUD Model

Open new window in notepad++ and write the basic syntax for crud_model class. Create class with name crud_model and make a constructor public function and within it call to parent cunstruct and load database as you can see in below section. 
Crud_model Class syntax:
class Crud_model extends CI_Model { }
Constructor Function Syntax: Add the below code within above crud_model class
public function __construct(){ parent::__construct(); $this-&gt;load-&gt;database(); }

CRUD Model-Insert Function

Below is the function with name insert_data(). I have created an array type variable $data and passed html form input fields names. In this array variable on the left side is the mysql table column name and on the right side is the html form input fields names. After this array type varibale initialization, I have called the codeigniter insert function. In this insert function of codeigniter two varibales we can pass first varibale is the mysql table name and second varibale is html form input fields name which is array type varibale.
Here is the insert function code add this code in crud model class.
public function insert_data() { $data=array( 'firstname'=&gt;$this-&gt;input-&gt;post('txtfname'), 'lastname'=&gt;$this-&gt;input-&gt;post('txtlname'), 'email'=&gt;$this-&gt;input-&gt;post('txtemail'), 'phone'=&gt;$this-&gt;input-&gt;post('txtphone'), 'address'=&gt;$this-&gt;input-&gt;post('txtaddress'), 'postal_code'=&gt;$this-&gt;input-&gt;post('txtpostalcode') ); $this-&gt;db-&gt;insert('person_detail',$data); $insert_id= $this-&gt;db-&gt;insert_id(); return $insert_id; }

CRUD Model-Read Data From MySQL Table

I will create a read function to fetch all data from mysql table. I will write a select query and pass it to a variable $sql. In the next line I will write code to execute this query using codeigniter query function and return the query result.
Below is the code of fetching data from mysql table using codeigniter. Add this code in crud_model class.
public function read() { $sql="select * from person_detail"; $query=$this-&gt;db-&gt;query($sql); return $query-&gt;result_array(); }

CRUD Model-Update MySQL Table Records

To update mysql table records we will use two functions. First function will be used to fetch unique data of mysql table each record by id.Second function will be used to update the mysql table record by its id.In the fisrt function I will use mysql select query and in second function I will use mysql update query.
Below is the code for update mysql table records using codeigniter.Add the code in crud_mdel class.
public function update_fecth($id) { $sql="select * from person_detail where id='$id'"; $query=$this-&gt;db-&gt;query($sql); return $query-&gt;row(); } public function update_rec($firstname,$lastname,$email,$phone,$address,$postcode,$id) { $sql="UPDATE `person_detail` SET `firstname`='$firstname',`lastname`='$lastname',`email`='$email',`phone`='$phone', `address`='$address',`postal_code`='$postcode' WHERE id='$id'"; $query=$this-&gt;db-&gt;query($sql); if($query) { return "success"; } else { return "failed"; } }

CRUD Model-Delete MySQL Table Records

For deleting mysql table records I will create a function to delete records with one parameter for id. I will write mysql delete query with where clause and pass it to a variable $sql.In the next line I will execute the query.
Below is the code for deleting mysql table records using codeigniter.Add the code in crud_mdel class.
public function delete_rec($id) { $sql="delete from person_detail where id='".$id."'"; $query = $this-&gt;db-&gt;query($sql); if($query){ return "success"; }else{ return "failed"; } }

CodeIgniter CRUD View

Now I will open a new window in notepad++ and write html tags to create a view for our crud model.In this view after closing the html form tag I have created an html table to read mysql table records.Write the code as it is we will perform the remaining work in our crud controller then it will display mysql table records in this html table.
Create a new crud_view.php file and write below code in it.
<html> <head> <title>CRUD Application</title> <link href="&lt;?php echo base_url();?&gt;asset/css/font-awesome.min.css" rel="stylesheet"></link> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"></link> <!-- Bulma Version 0.7.4--> <link href="&lt;?php echo base_url();?&gt;asset/css/bulma.min.css" rel="stylesheet"></link> <link href="&lt;?php echo base_url();?&gt;asset/css/login.css" rel="stylesheet" type="text/css"></link> <script src="&lt;?php echo base_url(); ?&gt;asset/jquery.min.js"></script> <style> .msg{ font-weight:bold; color:red; } .box{ margin-top:0% !important; } </style> </head> <body> <section class="hero is-success is-fullheight"> <div class="columns"> <div class="column is-8 is-offset-2"> <div class="box"> <form action="&lt;?php echo $__siteurl;?&gt;create" method="post"> <h1 class="title" style="color: black; text-align: center;"> CRUD Application Using CodeIgniter PHP</h1> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> <label class="label">First Name</label> <input class="input is-info is-large" name="txtfname" type="text" value="" /> </div> </div> <div class="field"> <div class="control"> <label class="label">Last Name</label> <input class="input is-info is-large" name="txtlname" type="text" value="" /> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> <label class="label">Email</label> <input class="input is-info is-large" name="txtemail" type="text" value="" /> </div> </div> <div class="field"> <div class="control"> <label class="label">Phone</label> <input class="input is-info is-large" name="txtphone" type="text" value="" /> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> <label class="label">Address</label> <input class="input is-info is-large" name="txtaddress" type="text" value="" /> </div> </div> <div class="field"> <div class="control"> <label class="label">Postal Code</label> <input class="input is-info is-large" name="txtpostalcode" type="text" value="" /> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> session-&gt;flashdata('response');?&gt; </div> </div> <div class="field"> <div class="control"> <input class="button is-info is-large is-fullwidth" name="btnsubmit" type="submit" value="Submit" /> </div> </div> <div class="field"> <div class="control"> </div> </div> </div> </div> </form> </div> <table class="table is-stripped is-narrow"> <thead> <th>ID</th> <th>Full Name</th> <th>Email</th> <th>Phone</th> <th>Address</th> <th>Postal Code</th> <th>Update</th> <th>Delete</th> </thead></table> </div> </div> </section></body></html>
Crud_model->read(); foreach($List as $row) { ?>
Update
Delete

CodeIgniter CRUD View For Update

This is the same html form as in above view but this view will be used to display mysql table unique record in each relevant input field. Create a new update.php file and add below code in it.
<html> <head> <title>CRUD Application</title> <link href="&lt;?php echo base_url();?&gt;asset/css/font-awesome.min.css" rel="stylesheet"></link> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"></link> <!-- Bulma Version 0.7.4--> <link href="&lt;?php echo base_url();?&gt;asset/css/bulma.min.css" rel="stylesheet"></link> <link href="&lt;?php echo base_url();?&gt;asset/css/login.css" rel="stylesheet" type="text/css"></link> <script src="&lt;?php echo base_url(); ?&gt;asset/jquery.min.js"></script> <style> .msg{ font-weight:bold; color:red; } .box{ margin-top:0% !important; } </style> </head> <body> <section class="hero is-success is-fullheight"> <div class="columns"> <div class="column is-8 is-offset-2"> <div class="box"> <form action="&lt;?php echo site_url('crud/update_recid');?&gt;" method="post"> <h1 class="title" style="color: black; text-align: center;"> CRUD Application Using CodeIgniter PHP</h1> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> <label class="label">First Name</label> <input class="input is-info is-large" name="txtfname" type="text" value="&lt;?php echo $crud_data-&gt;firstname;?&gt;" /> <input class="input is-info is-large" name="txtid" type="hidden" value="&lt;?php echo $crud_data-&gt;id;?&gt;" /> </div> </div> <div class="field"> <div class="control"> <label class="label">Last Name</label> <input class="input is-info is-large" name="txtlname" type="text" value="&lt;?php echo $crud_data-&gt;lastname;?&gt;" /> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> <label class="label">Email</label> <input class="input is-info is-large" name="txtemail" type="text" value="&lt;?php echo $crud_data-&gt;email;?&gt;" /> </div> </div> <div class="field"> <div class="control"> <label class="label">Phone</label> <input class="input is-info is-large" name="txtphone" type="text" value="&lt;?php echo $crud_data-&gt;phone;?&gt;" /> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> <label class="label">Address</label> <input class="input is-info is-large" name="txtaddress" type="text" value="&lt;?php echo $crud_data-&gt;address;?&gt;" /> </div> </div> <div class="field"> <div class="control"> <label class="label">Postal Code</label> <input class="input is-info is-large" name="txtpostalcode" type="text" value="&lt;?php echo $crud_data-&gt;postal_code;?&gt;" /> </div> </div> </div> </div> <div class="field is-horizontal"> <div class="field-body"> <div class="field"> <div class="control"> session-&gt;flashdata('response');?&gt; </div> </div> <div class="field"> <div class="control"> <input class="button is-info is-large is-fullwidth" name="btnsubmit" type="submit" value="Submit" /> </div> </div> <div class="field"> <div class="control"> </div> </div> </div> </div> </form> </div> </div> </div> </section> </body> </html>

CodeIgniter CRUD-Controller

create a new crud.php file and add the below code within the crud.php controller.
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Crud extends CI_Controller { public function __construct() { parent::__construct(); $this-&gt;load-&gt;database(); $this-&gt;load-&gt;helper('url'); $this-&gt;load-&gt;helper('form','url'); $this-&gt;load-&gt;model('Crud_model'); $this-&gt;load-&gt;library('form_validation'); $this-&gt;load-&gt;library('session'); $this-&gt;load-&gt;library('email'); } public function index() { $this-&gt;load-&gt;model('Crud_model'); $data['__siteurl'] =base_url() . "crud/"; $this-&gt;load-&gt;view('crud_view',$data); } public function create() { $insert=$this-&gt;Crud_model-&gt;insert_data(); $data['__siteurl'] =base_url() . "crud/"; $this-&gt;session-&gt;set_flashdata('response','<b style="color: red;">Data Saved Successfully</b>'); redirect(base_url().'crud/'); } //fecth data with idate public function update() { $id=$this-&gt;input-&gt;get('id'); $data['crud_data'] = $this-&gt;Crud_model-&gt;update_fecth($id); if(!empty($data['crud_data'])) { $this-&gt;load-&gt;view('update',$data); } } public function update_recid() { $id=$this-&gt;input-&gt;post('txtid'); $fname=$this-&gt;input-&gt;post('txtfname'); $lname=$this-&gt;input-&gt;post('txtlname'); $email=$this-&gt;input-&gt;post('txtemail'); $phone=$this-&gt;input-&gt;post('txtphone'); $address=$this-&gt;input-&gt;post('txtaddress'); $postcode=$this-&gt;input-&gt;post('txtpostalcode'); $res=$this-&gt;Crud_model-&gt;update_rec($fname,$lname,$email,$phone,$address,$postcode,$id); if($res=="success") { $this-&gt;session-&gt;set_flashdata('response','<b style="color: red;">Record Updated Successfully</b>'); redirect(site_url('crud/')); } else { $this-&gt;session-&gt;set_flashdata('response','<b style="color: red;">Failed To Update Record!</b>'); } } //delete function public function delete_byid() { $id=$this-&gt;input-&gt;get('id'); $res = $this-&gt;Crud_model-&gt;delete_rec($id); if($res == "success"){ $this-&gt;session-&gt;set_flashdata('response','<b style="color: red;">Data Deleted Successfully</b>'); redirect(site_url('crud/')); }else{ $this-&gt;session-&gt;set_flashdata('response','<b style="color: red;">Failed To Delete Record!</b>'); redirect(site_url('crud/')); } } }
If you want to download complete source code of this php codeigniter crud tutorial click o the below button. Download Source Code

No comments:

Post a Comment