Theo Theo - 4 months ago 7
PHP Question

php variable not recognised.

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.

Answer

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

Comments