Android PHP Insert Into MySQL DB Using Volley

By | December 14, 2014

In this tutorial we are going to see how to integrate PHP and MySQL database with android application , We use MySQL Database as database-server to store data and we use php Script to fetch and manipulate data from mysql database and show it on to android application .

androidphpinsert

Pre-Requisites
Firstly you have to set up php server.

Read : Setting PHP And MYSQL Server

   CREATE TABLE IF NOT EXISTS myorder (
         id int(100) NOT NULL AUTO_INCREMENT,
         item varchar(255) DEFAULT NULL,
         PRIMARY KEY (‘id’)
        ) 

1. PHP

1 a. DBConfig

file : db_config.php

<?php $mysql_hostname = "localhost"; $mysql_user = "root"; $mysql_password = ""; $mysql_database = "orders"; $db = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong"); mysql_select_db($mysql_database, $db) or die("Opps some thing went wrong"); ?>

1 b. Insert Script

Php Script to insert data into MySQL database

file : take_order.php

<?php error_reporting(0); include("db_config.php"); // array for JSON response $response = array(); if( !(empty($_POST['item_name']))) { $item_name=$_POST['item_name']; $result = mysql_query("INSERT INTO myorder(id,item) VALUES('','$item_name')"); if($result>0){
           $response["success"] = 1;
         }    
     else{
           $response["success"] = 0;
         }
     // echoing JSON response
     echo json_encode($response);
}

?>

1 c. API URL Call

URL http://api.tutorialsbuzz.com/Orders/take_order.php
Request Type POST
JSON Output On Success

{“success”:1}

On Failure

{“success”:0}

  • This is one of the best volley tutorial I recommend to other developer, simple and direct

  • Where is the ReadData class?

  • i have written tutorial on how to read data from mysql database check out below link
    http://www.tutorialsbuzz.com/2014/12/android-php-read-mysql-db-volley.html

  • it show me a null pointer exception at this line:::
    MyApplication.getInstance().addToReqQueue(postRequest);

  • How Can I store captured images in server php mysql using volley library

  • How can I store captured images in server using volley library?

  • This comment has been removed by the author.

  • where to put .php files? please, help.

  • place php files in server

  • Thank you

  • Hello. I'm new to android. I was wondering what id i want to add more than string. Such as first name, last name, username, email, and password. How will I do that in the HashMap?

  • I am trying to add an addition entry to the create php script. It works great as is, but when I add an additional entry, I get "data inserted successfully", but there is no update to the database. Below you can see where I added "order". When I add it, nothing is entered into the database.

    0){
    $response["success"] = 1;
    }
    else{
    $response["success"] = 0;
    }
    // echoing JSON response
    echo json_encode($response);
    }

    ?>

  • protected HashMap getParams() {
    // Posting params to register url
    HashMap params = new HashMap<>();
    params.put("user_name", user_name);
    params.put("user_email", user_email);
    params.put("user_password", user_password);

    return params;

    I think u was looking for this.

  • Hey, great tutorial, just one question. it show me a null pointer exception at this line:
    MyApplication.getInstance().addToReqQueue(postRequest);

    Can someone help me?

  • In Manifest file for <Application tag make sure you have set the name attribute with class name "MyApplication "

  • This comment has been removed by the author.

  • i was trying to add another entry and not error, but on database not update,
    can you help me please ?
    thanks,

  • how should I do to give an insert in more than one field in the table through the method $ _POST ?? I tried using the php above , including only variables , but did not succeed .

  • In db_config.php, you declared: $mysql_hostname = "localhost";
    But in MainActivity you declared:
    String url = "http://api.tutorialsbuzz.com/Orders/take_order.php&quot;;