CRUD stands for Create, Read, Update, and Delete. CRUD operations are basic data manipulation for the database. Create means insert data in the database using INSERT Query. Read means read data from the database using the SELECT Query. Update means edit data in the database using UPDATE Query. And Delete means delete data from the database using DELETE Query.
First, we create a database in MYSQL.
Create database crud;
Now, Create a table in crud database
Crud.php is the main file. In this file, we will include data connection, insert, read, update, delete.
In this file first, we will create a database connection class named database. This class contains the database connection code. Here, we define our database host, database name, username and database password.
Crud.php
Create insert.php file for HTML Form
To insert employee’s data in the database table we need to create an Html form. This form contains 7 input fields of employees First name, employees Last name, employees Gender, employees Email, employees Birthdate, department name, and profile picture.
Insert.php
Now, Insert data into database
First, we include the database class file in the insert.php file using include function.
Include crud.php;
Then check if the condition is set to submit. Then assigning the form value in variables using mysqli_real_escape_string function to strip data from input fields.
Include this code in crud.php file
Then employees submitted data then after this data store in PHP variables.
Now, This data is stored in a database using the insert query. So, we will create an insert method with insert query in crud.php file
In this function return true or false. If return true we print a success message and return false then we print a fail message.
Display all employees data in index.php file
We will fetch data from the database and display it in the index.php file. This file is our home page. This file will give the link of additional data. And every row of display data, we also give a link of edit data and delete data from the database.
Index.php file
Fetch data from the database
First, include database class from crud.php file in index.php file using include function.
Include crud.php;
Now, all employees data fetch from the database using a select query. So, we will write a read method with the select query in the crud.php file.
Now, this function call from the index.php file. So, add the below code in index.php.
$result = $database->read();
Using select query we select all data from the registration table. We use mysqli_fetch_array() function for fetch data from database.
Add this code in the index.php file.
Update employee data
First, we create an Html form for update data. Each row of data can be updated separately. Employee Id is passed in the URL of update.php.
In the below code, each row entry of data is fetched based on the id. The fetched data is displayed in the update form. We use read function to select data from the database and fetch result.
Html form for update
Now, we create an update function with an update query in the crud.php file.
Then after form submission, Before we will check the form is submitted or not, using if condition check update isset or not.
This code add in the update.php file.
Delete employee data using delete.php file
We delete employee data using the delete query. In the index.php file, we click on the delete link then user redirect in delete.php file with employee id in URL which one has deleted a record.
In the delete.php file, we include database class file is crud.php using include function. Then assign id which is passed in URL. Then we will call the delete method from delete.php file with id.
Now, we will create the delete method in crud.php file using the delete method. If a query is a success then return true else it returns false. If return true then we will redirect the user in the index.php file.
Just check and fill free to post a comment if have any query.
In this article, I will explain to you how to perform CRUD operation using CodeIgniter with MySql database. Note: Full CRUD operation will cover by more than one tutorial series. […]
Read more
Great article and examples! It saved a lot of time. It is just what I needed.
We are glad to be of help.
I seriously love your website.. Very nice colors
& theme. Did you develop this amazing site yourself? Please reply back as I’m
looking to create my own personal blog and would love to know where you got this from or just what the
theme is called. Thanks!
Great artical for Developer so Beginners are learning More From Your artical
Great Work Borther