1

I have the following php script where the user can rate a player from his Android phone and post that data in my server.

So:

<?php
  session_start();
  require "init.php";
  header('Content-type: application/json');

  error_reporting(E_ALL); 
  ini_set("display_errors", 1);

  $id = isset($_POST['player_id']);
  $user_id = isset($_POST['User_Id']);
  $best_player = isset($_POST['player']);
  $rate = isset($_POST['rating']);

  if($id && $user_id && $best_player && $rate){

    $sql_query = "insert into rating_players_table values('$id','$best_player','$rate','$user_id');";

    if(mysqli_query($con,$sql_query)){

        $don = array('result' =>"success","message"=>"Επιτυχής πρόσθεση παίχτη");
    }       
   }else if(!$best_player){

        $don = array('result' =>"fail","message"=>"Insert player name");

    }else if(!$rate){

        $don = array('result' =>"fail","message"=>"Rate player");

    }
 echo json_encode($don);

?>

However I get a message saying the $don variable inside the echo is not recognised.

The Android code that sends data is:

private void ratingPlayer(final String player, final int rating,final String UserId) {

    String tag_string_req = "req_register";

    StringRequest strReq = new StringRequest(Request.Method.POST,
            URL.URL_BEST_PLAYERS, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("Response", "Best player response: " + response.toString());

            try {
                JSONObject jsonObject = new JSONObject(response);
                if (jsonObject.getString("result").equals("success")) {
                    Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
                } else if (jsonObject.getString("result").equals("fail")) {
                    Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error", "Registration Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();

        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            // Posting params to register urls
            Map<String, String> params = new HashMap<String, String>();
            params.put("player_id", "");
            params.put("User_Id", UserId);
            params.put("player", player);
            params.put("rating", String.valueOf(rating));
            return params;
        }
    };
      // Adding request to request queue
      AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
   }

where

final String player, final int rating,final String UserId

the values I put in my edit texts fields in the Android phone.

What could be wrong?

Thanks,

Theo.

3
  • 1
    If the first if-statement is true but the one inside is false then $don is never initialized Commented Jul 16, 2016 at 18:52
  • I put the echo json_encode($don); inside if(mysqli_query($con,$sql_query)){..},but I get nothing. Commented Jul 16, 2016 at 18:56
  • Yes, it will never get that far. Look at my answer and see if it helps Commented Jul 16, 2016 at 19:02

1 Answer 1

1

This would be more correct

$id_isSet = isset($_POST['player_id']);
$user_id_isSet = isset($_POST['User_Id']);
$best_player_isSet = isset($_POST['player']);
$rate_isSet = isset($_POST['rating']);

if($id_isSet && $user_id_isSet && $best_player_isSet && $rate_isSet){

    $id = $_POST["player_id"];
    $user_id = $_POST['User_Id'];
    $best_player = $_POST['player'];
    $rate = $_POST['rating'];

    $sql_query = "INSERT INTO rating_players_table VALUES('$id','$best_player','$rate','$user_id');";

    if(mysqli_query($con,$sql_query)){

        $don = array('result' =>"success","message"=>"Επιτυχής πρόσθεση παίχτη");
    }       
   }else if(!$best_player){

        $don = array('result' =>"fail","message"=>"Insert player name");

    }else if(!$rate){

        $don = array('result' =>"fail","message"=>"Rate player");

    }
 echo json_encode($don);

?>

If you echo out isset($_POST["id"]) and it is true you will print "1" and not the actual post value

Sign up to request clarification or add additional context in comments.

4 Comments

$id = $_POST["id"]; $user_id = $_POST['User_Id']; $best_player = $_POST['player']; $rate = $_POST['rating'];
all the above posts are not recognised:(
Do you mean that isset() is false for all?
Sorry, it shall of course be $id = $_POST["player_id"] (exactly the same as in the isset -functions

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.