1

I am trying to access an individual player in the database, I am getting the response {"success":0,"message":"Required field(s) is missing"} but this is wrong and should be outputting a single player field.

I think the problem is with the php code but I am not certain. Any help is greatly appreciated, thanks.

php code -

<?php

require('db_connection.php');

// check for post data
if (isset($_GET["playerid"])) {
$playerid = $_GET['playerid'];

// get a player from week1 table
$result = mysql_query("SELECT *FROM week1 WHERE playerid = $playerid");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $player = array();
        $player["playerid"] = $result["playerid"];
        $player["score"] = $result["score"];
        $player["lastholeplayed"] = $result["lastholeplayed"];
        $player["overall"] = $result["overall"];

        // success
        $response["success"] = 1;

        // user node
        $response["player"] = array();

        array_push($response["player"], $player);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no player found
        $response["success"] = 0;
        $response["message"] = "No player found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no player found
    $response["success"] = 0;
    $response["message"] = "No player found";

    // echo no users JSON
    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);
}
?>

related java class -

public class InputScores extends Activity {


// Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object


    ArrayList<HashMap<String, String>> holesList;

    // url to get all products list
    private static String url_update_players = "http://192.168.2.4/realdeal/getplayer.php";

    // JSON Node names


    // products JSONArray
    JSONArray courseone = null;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (pDialog != null) {
            pDialog.cancel();
        }
    }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.updatescores);

    Intent i = getIntent();
    i.getStringExtra("playerid");

    //String playerid = i.getStringExtra("playerid");
    //System.out.println(playerid);
    //TextView myTextView = (TextView)findViewById(R.id.);
    //myTextView.setText(playerid);
    //String playerid = "999";
    new loadplayerdetails().execute();

}
/**
 * Background Async Task to Load all holes by making HTTP Request
 * */
class loadplayerdetails extends AsyncTask<String, String, String> {




    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(InputScores.this);
        pDialog.setMessage("Loading holes. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All holes from url
     * */
    protected String doInBackground(String... params) {

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url_update_players);

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    2);
            String playerid ="5";
            nameValuePairs.add(new BasicNameValuePair("playerid", playerid));



            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String response = httpclient.execute(httppost,
                    responseHandler);
            // you will get json string here

            // check here your getting json string in logcat.
            Log.d("response", response);



        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all players
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }


    }

}

}

2
  • Everything seems pretty much ok... maybe try removing the 2 from : List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); Commented Apr 22, 2013 at 23:45
  • Tried that but didn't work unfortunately. Commented Apr 22, 2013 at 23:52

1 Answer 1

1

You're posting your data not using "get".

You use $_GET when you make your call with everything in the URL:

http://blahblah.com/somescript.php?playerid=1234

In your code, you used $_GET to get it. When you're posting, as you did use $_POST:

if (isset($_POST["playerid"])) {
    $playerid = $_POST['playerid'];
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I changed to what you suggested but I am now getting what seems to be html style code in my response.
It sounds like the original problem is fixed, but you may have some debugging of your PHP code to do. You may want to make a quick web page that posts the data so you can test your php script separately.
Thanks I have tried testing this way though I am still unsure why it isn't working..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.