Breaking News

How to insert data into MySql Database from Android Application Step 2

This process can be used if you want to develop an App which is not Stand Alone  (access centralized database).
In this post we will understand how to move data from Android App to MySql Database. We are going to use an Android Studio with the help of XAMPP Server which will help to run Server Side Script like Php.
Again, we need a little bit knowledge on how to work with Php and MySql, but don't worry if you don't have that knowledge, this post will give you a light on it.
What to do is installation of Android Studio, XAMPP Server and Text Editor (Notepad++, Sublime Text, Bluefish, etc) on your Machine, after the Installation, follow the Main 5 steps, which will consists a lot of small steps on each.

  1. Create MySql Database
  2. Write Php code to interact with MySql Database.
  3. Develop Simple App to capture Data from the User.
  4. Connect Android App to MySQL Database.
  5. Insert Data to MySQL Database
Note: I'm using Windows 10 OS, Notepad++ and XAMPP Server.

Step 2: Write Php code to interact with MySql Database.

  • Open your Drive C (Local Disc C)
  • Open xampp folder
  • Open htdocs folder (Full Path  C:\xampp\htdocs. Here is the place where your php files will be saved)

  • Now, create a folder for saving your php files, so on htdocs create a folder, name it App (Now, Full Path will be  C:\xampp\htdocs\App)
  • Open your Local Server (Because I'm using xampp, I open it)
  • Click Start button to start Apache Server and MySQL Server (They are used to execute Php Code and MySQL Commands respectively. Green color on Apache and MySQL indicate that they are ok)
  • Open your Text Editor for writing Php codes (Because I'm using Notepad++, I open it)
  • According to our topic, we need two necessary files, first file is for Connection to the Database and second file is for Insertion of data to the Database. So, on your Text Editor write the following Php code to connect with Database (Code of the First File).
       <?php
              $server="localhost";
              $user="root";
              $pass="";
              $db="Information";
              $connect=mysqli_connect($server, $user, $pass, $db);
              if ($connect) {
                     echo"Database Connected Successfull....!!";
              } else {
                     echo "Database not connected.....!!!".mysqli_connect_error();
              }
      
?>

  • Now let us explain the meaning of each line of the above code.
  • <?php ....... ?> (This is the opening and closing tag of the php, so anything that will be passed in between these tags will be translated as a php code)
  • $server = "localhost"; (This line has four parts, First part is $server. This is a variable, on Php variable are declared by starting with dollar sign ($). Second part is (=). This is an assignment operator. It is used to assign the value of the right side to a variable of the left side. Third part is "localhost". This is a host name running the Database server. Fourth part is ( ; ). This is a terminate sign, it terminate the execution of the specified line)
  • $user = "root"; (This variable has the value "root". This means the default user accessing the database is root)
  • $pass = ""; (This variable has value of "". This means the password is null)
  • $db = "Information"; (This variable has the value "Information". This is the name of Database. We created it on previous Post, you can read it to get the full understanding on it)
  • $connect = mysqli_connect($server, $user, $pass, $db); (This variable has initialized a Function mysqli_connect as its value, and this Function takes four parameters discussed above. The purpose/meaning of this mysql_connect is to connect to the server called "localhost" where there is a Database called "Information", the owner of that database is called "root" who has a password of null (""))
  • if ($connect) {......} (This is the if statement that is used to test if the connection to the server and database is successful. If this is true, it will display a message "Database Connected Successfull....!!" otherwise it will check on other statements)
  • echo "......."; (This is used to display/print information to screen)
  • else {........} (This is used to process the information that didn't obey the condition on if(.....). It acts as a negation of if statement, means if the result of the if condition is false, here it will be true)
  • echo "Database not connected.....!!!".mysqli_connect_error(); (This line has new two parts, First part is ( . ) This is a concatenation operator used to join statements, on this statement it concatenate "Database......." and mysqli_connect_error() function. Second part is mysqli_connect_error(); This is function that will display exactly what the error caused by connection to the database. Sometimes a developer can get an error and don't know what the source of that error, so this function will help him/her to understand and fix it)
  • Finally, save it in the folder located on Drive C:\xampp\htdocs\App and name it as connection.php

  • Now, let us run it to test if the connection is successful or not.
  • Open your browser, on the Address bar, type the following path and press enter.
  • localhost/App/connection.php (If connection is successful it will display the message Database Connected Successfull....!! and if connection is not success it will display the message Database not connected.....!!! )

As we said above that this part needs two files, the above code was for the first file which connect to the database and now let us write the code for the other file which will take our data and store them to the database. (Code of the Second File)
  • Open new file on your Text Editor. Follow the following steps create new file.
  • Click File menu
  • On displayed sub menu, Click New (New file will be opened) or press Ctrl + N.
  • Now type the following codes.
       <?php
             include("connection.php");
             $name="Source of Concept";
             $phone="+255 000111222";
             $addr="https://sourceofconcept.blogspot.com/";
             $check=mysqli_query($connect, "insert into Student values ('$name', '$addr', '$phone')");
             if ($check) {
                   echo "Data Inserted Successfully....!!!";
             } else {
                   echo "Data not inserted successfull....!!!";
             }
      
?>

  • Now let us explain the meaning of each line of the above codes.
  • include("connection.php"); (This means, an include() function will take all the code from file called connection.php and import in this file. So go and comment (//) the message //Database Connected Successfull....!! to avoid to display when we run this file)
  • $check=mysqli_query($connect, "insert into Student values......."); (This variable has initialized a Function mysqli_query as its value. The purpose of this Function is to execute MySQL commands and it takes two parameters, First is $connect which means, from which connection our data we want to push? And Second is "insert into Student values....... the MySQL command which insert the data to the Table called Student, which has three columns named as Name, Address and Phone and from that columns we put data named as 'Source of Concept', '+255 000111222' and 'https://sourceofconcept.blogspot.com/' respectively. We created this Table on the step 1)
  • Note: The above data will be replaced by the data that will come from the Android App, here is just for testing!
  • Finally, save it on the folder lacated on Drive C:\xampp\htdocs\App and name it as insert.php

  • Now, let us run it to test if our data has been stored to Database or not.
  • Open your browser, on the Address field, type the following path and press enter.
  • localhost/App/insert.php (If data will be inserted successful it will display the message Data Inserted Successfully....!!! and if not, it will display the message Data not inserted successfull....!!!)
  • As you can see the message say data has been inserted to the Database. Let us confirm if its true! Type the following path on Address bar and press enter to get to the Databases.
  • http://localhost/phpmyadmin/ 
  • On left side, Click on information (Database name)
  • Click on student (Table name)
  • Then you will see all data that has been inserted in your Database.
  • We are done!!
  • Until here, we have create a Database called Information and write some Php scripts that interact with our Database and we have complete two steps among the Main 5 Steps explained above. In the coming post, we will develop a simple app to capture the data from user...!!!
Please, Comment and Share with your friends!!

Thank you..!!


2 comments:

  1. Great, you're doing good stuffs. Waiting for more to come

    ReplyDelete