Android Insert Data to MySQL Database Using PHP (Android , MySQL , PHP , PDO)

We will see here how to insert android data to MYSQL database using PHP with PHP Data Objects ( PDO )  method. Here , I used example of Firebase Cloud Messaging (FCM) . The FCM SDK generates a registration token for the client app instance. To send push notification to client app first  you need to store this registration token in your database during app installation. We will discuss here in detail about data insertion from android to mysql/php,  why to use PHP Data objects(PDO).

You can store any data like EditText field data like name , email and other data like android device id, mac address etc to insert from android app client to mysql database. But I will discuss here only about how to insert FCM registration token into mysql database. If you want to know more about FCM and push notifications then see the post FCM Send Push Notification Using PHP .

MyInstanceIDListenerService.java:

This class extends FirebaseInstanceIdService and generate unique registration token for a android device. We have to send this registration token to server. See the following code snippet.

You need to connect your app client to web server. Here I used XAMPP server for connection between app client to php server to better understand. We have to make HTTP POST request here to send data from app to server. Here HttpPost(url) used to connect and send data.  BasicNameValuePair has two parameters . POST name and it’s value. Here in code we are sending ‘fcmid’ as a HTTP POST reqeust and it’s  value ‘refreshedToken’.

The method httpclient.execute(httppost, responseHandler) execute the HTTP POST request and send data to server which returns the response String. We can test with response string whether data received on server side or not.

PHP PDO(PHP Data Objects):

The PHP Data Objects (PDO) extension is interface for accessing databases in PHP. There are many advantages of PDO over MYSQL and MYSQLi. The main two advantages of PDO are:

  1. PDO supports 12 different database systems. Suppose for any reason you changing database(mysql to other like Oracle,  ODBC,  PGSQL, Microsoft SQL Server ) then you have to also change in code !. But if you are using PDO then there is change in only database connection code. No necessary to change entire code of your project.
  2. If you are using prepared statements in PDO then you can easily protect your data from SQL injection attacks.

We strongly recommend you that don’t use mysql_query()  syntax to execute SQL queries. Hence it is not secure. Migrate your code to PDO and use latest version of PHP i.e. PHP7 .

connectandroid.php:

If you are using other than mysql database then there is only little change in connection like:

insertfcm.php:

Using $_POST[‘fcmid’] you will get registration token of app client. Then insert this token in database. See the above code I have used prepared statement to secure data from SQL injection attacks. Send the response to app client to acknowledge that you have received token from client app.

The beginTransaction() and rollback() methods are used to rollback your actions if any issue were found. for example, if you sending and inserting more than thousand of user input data like emails, names in database and suddenly some issue occurred like no internet connection or load on server then it automatically rollback your last sql query execution. Later,  you can send or execute same action without any worry about last failed action !.

In this way you can insert FCM registration token in database. If you want to send push notifications to all this registration token then you can see the post FCM Send Push Notification Using PHP . If you have any question regarding this post then feel free to comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *