0

I am creating an android application and submit the user details from Android application to php at server side by JSON. But at server side I am not getting the JSON data. At server side php the JSON comes from android application is seems null. I am using POST method for this. I know that my POST method code is wrong in PHP at server side. But I have no idea to solve this.

Here is my PHP code for getting JSON. Every time I get same message:

Required fields missing

Code:

<?php

/*
* Following code will create a new user row
* All user details are read from HTTP Post Request
*/

// array for JSON response
$response = array();
print_r($_POST);
// check for required fields
if (isset($_POST['Firstname']) && isset($_POST['Lastname']) && isset($_POST['Username'])     && isset($_POST['Email']) && isset($_POST['Password']) && isset($_POST['Country']) &&     isset($_POST['Mobile'])) {
echo('bhargavi');
$Firstname = $_POST['Firstname'];
$Lastname = $_POST['Lastname'];
$Username = $_POST['Username'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
$Country = $_POST['Country'];
$Mobile = $_POST['Mobile'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// mysql inserting a new row
$result = mysql_query("INSERT INTO     users(Firstname,Lastname,Username,Email,Password,Country,Mobile)     VALUES('$Firstname','$Lastname','$Username','$Email','$Password','$Country','$Mobile')");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "User successfully Registered.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Bhargav Thanki
  • 4,924
  • 2
  • 37
  • 43
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 11 '14 at 21:31
  • okay I will do that but I cant even reach at that line my code is terminated after checking first if condition.Compiler does not compile it as my first if condition became false. – Bhargav Thanki Nov 11 '14 at 21:35
  • Are you getting any data at all? – vcanales Nov 11 '14 at 21:35
  • no.data is coming from android application.at android side I checked that data is coming to php in json but at at server side I cant get it. – Bhargav Thanki Nov 11 '14 at 21:38
  • Start simply. Comment out all lines of code except for `print_r($_POST);` and see if that works first, then go from there. – Jay Blanchard Nov 11 '14 at 21:38
  • it shows only array() – Bhargav Thanki Nov 11 '14 at 21:39
  • 1
    Are you sure youre not using GET http request type? if print_r($_POST); prints empty, it means youre not getting any data... to test for GET: print_r($_GET); – Nu Gnoj Mik Nov 11 '14 at 21:40
  • I am sure I am using POST method. – Bhargav Thanki Nov 11 '14 at 21:41
  • Well, then its not your PHP that is wrong. – Nu Gnoj Mik Nov 11 '14 at 21:41
  • 1
    It's basically a duplicate of your other question here: https://stackoverflow.com/questions/26867282/can-not-send-data-from-android-application-to-remote-database/26867434 Watch the answer there and try it. – Alexey Nov 11 '14 at 21:42
  • ok I will check again at android side – Bhargav Thanki Nov 11 '14 at 21:43

1 Answers1

0

Probably you should decode the json string before checking $_POST['Firstname'] etc...

Let's say you send from the app a string called data. On php you should do something like that:

$data = json_decode($_POST['data']);
and then check if $data contains the data you neeed by doing:
if (isset($data->Firstname) && isset($data->Lastname) etc...){
...
...
}

If I can give you an advice it would be better if you check data integrity directly from your app instead of doing it on your server, it would give the chance to spare some server resources...

As Jay Blanchard already mentioned don't use mysql like that or you'll be exposed to mysql injection...

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Mario Norato
  • 1,871
  • 2
  • 12
  • 12
  • First thing I can advise you to do is to print on the screen the content of the $_POST variable by writing: var_dump($_POST); or maybe even better (seen that you0re sending data from the app and probably don't access the page from the browser) do something like that: echo json_encode($_POST) and print on the logCat the result you get. – Mario Norato Nov 11 '14 at 21:50
  • I can get json array in logcat but in php page I get nothing just []. – Bhargav Thanki Nov 11 '14 at 22:12
  • Can you please post the code of your app where you send the data to the server? – Mario Norato Nov 11 '14 at 22:16