CRUD Operation in MySQL Using PHP, Volley Android – Insert Data
It is known that we can use MySQL to use Structure Query Language to store the data in the form of RDBMS. SQL is the most popular language for adding, accessing and managing content in a database. It is most noted for its quick processing, proven reliability, ease, and flexibility of use. The application is used for a wide range of purposes, including data warehousing, e-commerce, and logging applications. MySQL provides a set of some basic but most essential operations that will help you to easily interact with the MySQL database and these operations are known as CRUD operations.
In the previous article, we have seen creating a new SQL database in the PhpMydmin service. In this article, we will perform the Insert data operation. Before performing this operation first of all we have to create a new PHP script for adding new data to that database in our SQL Database.
Prerequisite: You should be having Postman installed in your system to test this PHP script.
Create a new PHP script for adding new data to that database in our SQL Database
We will be building a simple PHP script in which we will be used to add data to our SQL table which we have created in our previous article. Using this script we will be adding data to our SQL table.
Step by Step Implementation
Step 1: Start your XAMPP server which we have seen starting in the previous article
In the previous article, we have seen starting our XAMPP server and we also have created our database. In this article, we will be creating a script for adding data to our database.
Step 2: Navigate to xampp folder
Now we have to navigate to C drive in your pc and inside that check for the folder name as xampp. Inside that folder navigate to htdocs folder and create a new folder in that and name it as courseApp. Inside this folder, we will be storing all our PHP scripts. Now for writing your PHP script we can use any simple text editor. I am using VS code. After creating this folder we simply have to open this folder in VS code.
Step 3: Creating a new PHP file
After you open your folder in VS code, inside that folder we have to press a shortcut key as Ctrl+N our new file will be created. We have to save this file with the name addCourses.php and add the below code to it. Comments are added in the code to get to know in more detail.
Step 4: Getting URL for our PHP script
For getting the URL for our PHP script we simply have to type localhost in our browser and we have to append it with our folder name and file name. You will get to see the URL highlighted below:
Now we will be adding data in our SQL table with this URL in postman.
Step 5: Testing our PHP Script in Postman
For testing your PHP script select the POST method in postman as we will be posting data to our SQL table and inside the URL section add the above URL. After adding the URL. Now click on the Body tab which is shown in the below screenshot and inside that select x-www-form-urlencoded and after that add the parameters in the below section as shown in the screenshot. Make sure the key which you are entering must be the same as that we have used for naming our columns in our SQL table. After adding all the data. Now click on Send option to send data to our SQL table.
After sending this request our data has been added to our SQL table. You can get to see the data added in the PhpMyAdmin console in the below screenshot.
Insert Data Operation
In the upper part, we have created a PHP script for adding data to our SQL table. Along with that we have also tested that script by adding data to it. In this part, we will integrate that in our Android App and add data to our SQL table from our Android app.
What we are going to build in this article?
We will be building a simple application in which we will be simply adding course details from a simple form in our SQL table which we have created. For performing this operation we will be using the Volley library which is used for JSON parsing in Android. Below is the video in which we will get to see what we are going to build in this article.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add the below dependency in your build.gradle file
Below is the dependency for Volley which we will be using to get the data from API. For adding this dependency navigate to the app > Gradle Scripts > build.gradle(app) and add the below dependency in the dependencies section.
After adding this dependency sync your project and now move towards the AndroidManifest.xml part.
Step 3: Adding permissions to the internet in the AndroidManifest.xml file
Navigate to the app > AndroidManifest.xml and add the below code to it.
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Now run your app and see the output of the code.
You can get to see the data that has been added to your SQL table in the below screenshot.
Please Login to comment...