React Native Fetch API Tutorial to Insert Into MySQL Database Using PHP iOS Android Example

React Native’s Fetch API is the most common and default Networking API used to send data from react native application to online server side MySQL database using PHP. Fetch API is mostly similar as XMLHttpRequest API. The Fetch API provides a JavaScript interface for accessing and transferring different parts of data using a secure HTTP pipeline. So in this tutorial we would going to create a react native app along with Networking Fetch API Tutorial to Insert Into MySQL Database Using PHP iOS Android Example. So let’s get started .

What we are doing in this tutorial :

We are simply inserting data from react native iOS and Android app to our online server side MySQL database using PHP.

  • Database Used in this tutorial : MySQL
  • Server Side Scripting Language : PHP
  • API used in APP : Fetch Networking API

Contents in this project React Native Fetch API Tutorial to Insert Into MySQL Database Using PHP:

1. Create a MySQL Database and Table on your server .

Make New MySQL database or use your existing one. Create a Table inside MySQL database named as Products_Table along with 4 columns product_idproduct_nameproduct_number and product_details. We would make the product_id as primary key with Auto Increment. See the below screenshot for more details.


Screenshot of Table after creating :

2. Create Server side code using PHP to receive data from App and Insert into MySQL database table :

We have to create 2 individual files to perform this functionality First is DBConfig.php and Second is Insert_Product.php .

DBConfig.php : This file contain all the important information about your MySQL server like Server Host name, server password, server user name and database name.

Insert_Product.php : This file would receive the sent data from Fetch API and insert that data into MySQL database.

Code for DBConfig.php File :

Code for Insert_Product.php file :

3. Import StyleSheet, View, TextInput, Text, ActivityIndicator and TouchableOpacity component in your react native project.

4. Create constructor() in your project’s class and make 4 States inside it named as Product_Name, Product_Number, Product_Details and ActivityIndicator_Loading.

Product_Name : Holds the Product name entered inside Product name TextInput.

Product_Number : Holds the Product Number entered inside Product Number TextInput.

Product_Details : Holds the Product Details entered inside Product Details TextInput.

ActivityIndicator_Loading : Used to show and hide the Activity Indicator .

5. Create a function named as Insert_Data_Into_MySQL(). Inside this function we would create create the Fetch Networking API code to send the data from App to MySQL database. We would fist set the ActivityIndicator_Loading as true to show the ActivityIndicator on middle of screen while sending data.

  • Method Used to send data using Fetch API is POST.
  • headers : Tells the server that the Data is in JSON Object form.
  • body : Contain all the Data with a unique identifier.

6. Create a Root View in render’s return block. This View would hold all the TextInput and TouchableOpacity component.

7. Create 3 TextInput component inside the Root View.

placeholder : Is used to show hint inside the Text Input.

style : Is used to call the custom CSS style class.

underlineColorAndroid : Hide the TextInput’s own underline.

onChangeText : Store the TextInput inside typed text into State.

8. Create a Custom Button using TouchableOpacity component. We would call the Insert_Data_Into_MySQL() function on this button onPress event.

8. Create ActivityIndicator inside a Curly bracts block. The ActivityIndicator will only visible on if the ActivityIndicator_Loading state value is True. So it will show on screen at Data inserting time and when the response came from server then it will hide the ActivityIndicator. We are using the Ternary operator to perform this functionality.

9. Create CSS Style for All components.

10. Complete source code for App.js File :

Screenshot in Android device :

Fetch API Screenshots in iOS device :