PHP CRUD OPERATION


In this tutorial, we are going to learn PHP CRUD OPERATION very important topic. I am going to show you how to create a PHP crud operation with the MySQL database. So, this article is all about PHP and MySQL database. Well, To understand PHP and MySQL you need to have a solid understanding of a create, read, update and delete data from the database.

PHP CRUD

To create this application we need to have a basic understanding of PHP and MySQL database. Let’s get started and see how to create this beautiful PHP CRUD OPERATION with MySQL database.

So the first you need to create a design first then add style and then add crud functionality in your application. So I am going to first create a design of this application. Now following is the structure of this application.

Now as you can see, In this file structure I have crud folder( name of the application), then I have PHP folder inside of it. In this PHP folder, we gonna create some PHP files to achieve a result. Outside of the PHP folder and inside of crud folder, I have index.php and style.css file.

Now let’s see what kind of stuff we have in index.php file to design this application.

Index.php

As you can see, In this file we simply created an HTML design. I added two files at the start of this file which we will create a latter. In this file, I added simple text boxes to get the values from the user and perform actions. Then I have buttons which represent Create, Read, Update and Delete.

Now ones I have data I will show them in the table. Let’s quickly create a style.css file to style this file.

Style.css

Actually, I am not gonna add too many classes and style because I have the bootstrap library. Using bootstrap I will make it more attractive using predefined CSS classes.

Now let’s create a component.php file to create component dynamically. To create component more than ones I am gonna put them in the function and use it as many time as I want. Don’t forget this file is created in the PHP folder. If you are going to change his path don’t forget to change the file path from the code as well.

component.php

In this file, I have created two functions. Using inputElement() function I am going to create input tabs with font awesome icons and using buttonElement() function I will create buttons. I passed parameters to this functions so when we call it we can specify different values to that button function.

Ones we have the design let’s create a new database. I am not gonna create a new database manually instead I will make it using PHP Script. So when you execute this application database and table automatically create.

Now let’s look at the database file.

db.php

In this file, I have a function Createdb(). Using this function I am gonna create a new database with a new table. If you have a different username and password change the variable value. In this function, I have SQL commands to create a new database and table.

To create a new database with a table just call this file with Createdb() function. I am gonna call this function in the operation.php file. In this file we gonna do create, read, update and delete operation. Well, I think this is the best time to create this file. So let’s see what we have in this file.

Operation.php

Now, In this file, I had created a crud operation. I am using different functions to make this possible. In this file, you can find the update, read, delete, deleteAll and Create functions. In this file, I had just created first if statement and add isset() function. If the button has valued the execute if block. In this, if block I put the functions to perform different operations.

Keep in mind all these files are linked together so don’t put them different path if you do then make sure you change the path code inside the require_one() function.

Now to update the values I am going to first put that values in the textbox and then update it. So when you click on the edit button all values should be in the textbox. To do that I am using Javascript. Using JavaScript I will create an event on the edit button and put all values in the textbox.

If you take a look at the index.php file I had specified id to the table rows data. So we can identify which data belongs to which id. Using this technique I will display all the table data in the textboxes.

Now let’s see how to do this. So create the main.js file.

main.js

Now when you click on the edit button, you can see table data display in the text boxes according to the id.

So now, Your application is ready to serve. To execute your application just specify the localhost and the filename index.php.

I hope you like PHP CRUD OPERATION if so don’t forget to comment. To download, this project, Click on this link to download from the GitHub.

To watch this tutorial, click on this link.

Now that is all for now. I will see you in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *