I have posted tutorial on this topic about a year ago and few peoples are facing problems understanding the code. So today i am making the same tutorial again with more explanation, So all the new and old users can understand it easily. In this tutorial we would create react native application with user registration form containing 3 TextInput fields User Name, User Email and User Password. After submitting the values the data goes to our web server. Where we would receive the data using PHP API and using the MySQL queries we would insert the data into MySQL database. So let’s get started :).
NOTE: We are using 000webhost.com website for testing our PHP API + MySQL database. This website is amazing guys and gives us almost every facility to test our applications using free online server. They gave us free sub domain of which i am using in this tutorial. Our testing sub-domain is reactnativetestapi.000webhostapp.com/ .
Contents in this project React Native User Registration using PHP MySQL Database PART-1 Android iOS Tutorial:
1. Creating MySQL Database :
Log-In into your PhpMyAdmin control panel and create a database or you can use your own old already created MySQL database. In my case i have created the MySQL database and my database name is id10676035_react_native_test_database. You can see in below screenshot how my database looks like.
1. Select your database, In which you want to create table and write your Table name in create table box with column number. I am creating a table named as user_data_table with 4 columns. I will explain all the columns name in next step. Now press the Go button to create new table.
2. In the name field you have to enter 4 columns name id, user_name, user_email and user_password. Set their Length/Values to 255 and select ID as Auto_Increment and Set ID key to Primary. For exact match see the below screenshot. You could click on the Screenshot to see it in bigger size. Now hit the Save button to create table with all details.
1. Now we need to make a .php extension file using Notepad or Notepad++. This PHP file would connect our MySQL database table to our react native app. Open the Notepad or Notepad++ and paste all below code and save the file with registration_api.php name. After done creating this file you have to upload this file into your Web hosting server using their File Manager our using FileZilla ftp client. Here is my domain URL after uploading this file on my own testing web server. https://reactnativetestapi.000webhostapp.com/ . After opening the domain you’ll see my file is there. We would use this file URL to test our server.
Source code for registration_api.php file:
//Put your own hosting server HOST name here.
$HostName = "localhost";
//Put your own MySQL database name here.
$DatabaseName = "id10676035_react_native_test_database";
//Put your own MySQL database User Name here.
$HostUser = "id10676035_root";
//Put your own MySQL database Password here.
$HostPass = "1234567890";
// Creating connection.
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
// Getting the received JSON into $Received_JSON variable.
$Received_JSON = file_get_contents('php://input');
// decoding the received JSON and store into $obj variable.
$obj = json_decode($Received_JSON,true);
// Populate User name from JSON $obj array and store into $user_name variable.
$user_name = $obj['user_name'];
// Populate User email from JSON $obj array and store into $user_email variable.
$user_email = $obj['user_email'];
// Populate Password from JSON $obj array and store into $user_password variable.
$user_password = $obj['user_password'];
//Checking User entered Email is already exist or not in MySQL database using SQL query.
$CheckSQL = "SELECT * FROM user_data_table WHERE user_email='$user_email'";
// Executing SQL Query.
$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
$Duplicate_email = 'Email Already Exist, Please Try Again With Different Email.';
// Converting the message into JSON format.
$Duplicate_email_Json = json_encode($Duplicate_email);
// Echo, Printing the message on screen.
echo $Duplicate_email_Json ;
// Creating SQL query and insert the record into MySQL database table if email dose not exist in database.
$Sql_Query = "insert into user_data_table (user_name,user_email,user_password) values ('$user_name','$user_email','$user_password')";
// Show the success message.
$MSG = 'User Registered Successfully' ;
// Converting the message into JSON format.
$json = json_encode($MSG);
// Echo, Print the message on screen.
echo $json ;
echo 'Try Again';
Important Note: You have to put your own hosting server details in this file like your database HostName, Database name, Database Username, Database Password. All these details will be found on your hosting Cpanel. Get ready for NEXT part of this tutorial to learn about react native application coding.