React Native Insert Update Display Delete CRUD Operations MySQL DB

This tutorial is the fully advanced tutorial in react native because in this tutorial we would going to implement CRUD operations in react native android iOS applications. CRUD operations consist four type of major functions Add, Edit(Modify), Delete and View. So we would going to create a react native app with Insert Update Display Delete CRUD Operations From MySQL database using PHP.

What we are doing in this project :

We are creating a react native app with react-navigation library because without the react-navigation we cannot add multiple activities in our project. Now would insert student records in online MySQL database using PHP from our react native application. After that we would retrieve all those records into ListView in next activity. Now we would open the selected record into next activity and automatically fill each data into TextInput. We would also gives the option to delete the current selected data. So let’s get started .

Project Description File Structure:

PHP Files in this project :

  1. DBConfig.php
  2. InsertStudentData.php
  3. ShowAllStudentsList.php
  4. UpdateStudentRecord.php
  5. DeleteStudentRecord.php

Activity classes in this project created in App.js file

  1. MainActivity
  2. ShowStudentListActivity
  3. EditStudentRecordActivity

Contents in this project Insert Update Display Delete CRUD Operations From MySQL DB in react native using PHP :

1. Create a fresh MySQL database in your online hosting server.

2. Create a Table named as StudentDetailsTable with 5 columns student_idstudent_namestudent_classstudent_phone_number and student_email. Now we would make the student_id as Primary key with Auto Increment. See the below screenshot for more details. This would be our main table and we would perform all CRUD operations on this table from our MySQL database.


3. Create 5 PHP Script files to Receive and Insert, Display, Update and Delete data from MySQL database from React Native app:

We would make 5 PHP files named as DBConfig.phpInsertStudentData.phpShowAllStudentsList.phpUpdateStudentRecord.php and DeleteStudentRecord.php . You need to upload all these files to your hosting server.

  1. DBConfig.php : This file consist all the MySQL database connection configuration like MySQL database host name, MySQL database name, MySQL database username, MySQL database password.
  2. InsertStudentData.php : This file would receive the sent data from react native app and insert that data into MySQL table.
  3. ShowAllStudentsList.phpThis file would convert all the existing present MySQL database data into JSON format.
  4. UpdateStudentRecord.php : This file would update the existing opened record in app.
  5. DeleteStudentRecord.php : This file would delete the existing open record in react native app.

1. Code for DBConfig.php.php file :

2. Code for InsertStudentData.php file :

3. Code for ShowAllStudentsList.php file :

4. Code for UpdateStudentRecord.php file :

5. Code for DeleteStudentRecord.php file :

4. Start a new react native project or open your existing project’s App.js file.

5. Open your project’s folder in Command Prompt / Terminal and install react-navigation library. by typing 
npm install save reactnavigation command. You can read my this tutorial to understand how activity structure would works.

6. Import StyleSheet, View, Alert, TextInput, Button, Text, Platform, TouchableOpacity, ListView and ActivityIndicator component in your project.

7. Import StackNavigator from react-navigation library.

8. Create a class named as MainActivity in your project. This would be your first screen of app with User registration from.

navigationOptions : Used to set the Activity title which display inside the Action Title bar.

constructor() : Creating 4 States inside the constructor() named as TextInput_Student_Name, TextInput_Student_Class, TextInput_Student_PhoneNumber, TextInput_Student_Email . Each state is used to hold the entered value from TextInput.

InsertStudentRecordsToServer() : This function would run the react native’s fetch() web api and send the all TextInput entered value to server and after successfully insertion it will show the response message coming from server into Alert dialog box.

GoTo_Show_StudentList_Activity_Function() : In this function we would open the ShowStudentListActivity represent as Second activity.

Screenshot of MainActivity :

9. Create class named as ShowStudentListActivity. This class would display all the entered students records in ListView.

constructor() : We are creating a Boolean type State named as isLoading. This state is used to show and hide the ActivityIndicator after done loading JSON data into ListView. If the isLoading value is true then it will show the ActivityIndicator and if the isLoading value is false then it will hide the ActivityIndicator.

navigationOptions : Used to set the Activity Title shows inside the Title Action bar.

componentDidMount() : This is a inbuilt function of React Native. This function would automatically calls on activity start up time. We would write the Fetch() Web code inside componentDidMount() method so when the this activity opens then it will start a web call and fetch the JSON data from server and display inside ListView. We are only displaying Student Names inside this ListView.

GetStudentIDFunction() : This function is very important for this class because in this function we would send the student_id, student_name, student_class, student_phone_number and student_email address to next EditStudentRecordActivity on ListView item onPress event. When use clicks on any ListView item then it will automatically get all the values form current selected record and navigate to next activity where it will be automatically received and filled into TextInput.

ListViewItemSeparator() : This function would render a separator line between each item of ListView.

Screenshot of ShowStudentListActivity :

10. Create a class named as EditStudentRecordActivity. This class is used to Edit the current open record to MySQL database. This class is also used to delete the current selected opened record.

constructor : We would create 5 state named as TextInput_Student_ID, TextInput_Student_Name, TextInput_Student_Class, TextInput_Student_PhoneNumber, TextInput_Student_Email in constructor(). Each state is used to store and hold the already sent data from previous activity.

componentDidMount() : Storing all the sent value from previous activity into State using this.props.navigation.state.params .

navigationOptions : Setting up the activity title shows inside the Title Action bar.

UpdateStudentRecord() : This function would get the all TextInput entered value and send them to server using fetch() API where they will update the existing record using ID.

DeleteStudentRecord() : This function would get the only Student ID from TextInput_Student_ID and send the only TextInput_Student_ID to server . On server we would delete the current record according to ID.

11. Create StackNavigator and define all your activity classes inside it.

12. Create CSS Style classes for All activities.

13. Complete source code for App.js File :

Screenshot in Android device :

Insert Update Display Delete CRUD Operations Screenshot in iOS device :