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.
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.
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
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.
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.
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 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.
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.
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:
Constructor Function Syntax:
Add the below code within above crud_model class
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.
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.
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.
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.
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.
ID | Full Name | Phone | Address | Postal Code | Update | Delete |
---|
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.CodeIgniter CRUD-Controller
create a new crud.php file and add the below code within the crud.php controller.
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