php变量无法识别。

时间:2016-07-16 18:49:16

标签: php android mysql

我有以下PHP脚本,用户可以从他的Android手机评价播放器并将该数据发布到我的服务器中。

所以:

#!/usr/bin/env perl

use strict;
use warnings;
use POSIX qw(strftime);

# data from user input
my $start_dt = '01-JUL-16';
my $end_dt   = '12-JUL-16';

# convert user input to upper case ("uc") in case the input is mixed
# and/or lower case. for lower case use "lc" and adjust the line
# with "%MONTH_NUMBERS = ..." accordingly.
$start_dt = uc($start_dt);
$end_dt   = uc($end_dt);

# create a hash with "JAN=>1, FEB=>2, MAR=>3, ..." in your current locale
my %MONTH_NUMBERS = map { uc( POSIX::strftime( "%b", 0, 0, 0, 1, $_ - 1, 95 ) ) => $_ } ( 1 .. 12 );

# returns "now" in the format 'YYYY-MM-DD' (e.g. for today '2016-07-16')
my $dt = POSIX::strftime( '%Y-%m-%d', localtime() );

# convert $start_dt and $end_dt into 'YYYY-MM-DD'.
# This assumes your input years are in the range 00-99 and actually mean 2000-2099.
# If that's wrong, then you need to adjust the "2000+$3" part.
$start_dt =~ s/(\d\d)-(\D\D\D)-(\d\d)/ sprintf('%d-%02d-%02d', 2000+$3, $MONTH_NUMBERS{$2}, $1) /e;
$end_dt =~ s/(\d\d)-(\D\D\D)-(\d\d)/ sprintf('%d-%02d-%02d', 2000+$3, $MONTH_NUMBERS{$2}, $1) /e;

# now the values are:
#   dt       : 2016-07-16
#   start_dt : 2016-07-01
#   end_dt   : 2016-07-12

# you can compare them now with simple string operations, i.e. 'le', 'eq', 'gt' and so forth:
if ($end_dt gt $dt) {
    print "end date is in the future\n";
} elsif ($start_dt le $end_dt) {
    print "start date is less than or equal to end date\n";
} else {
    print "start date is greater than end date\n";
}

但是我收到一条消息,说回信中的$ don变量无法识别。

发送数据的Android代码是:

<?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);

?>

其中

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);
   }

我在Android手机的编辑文字字段中输入的值。

可能出现什么问题?

谢谢,

西奥。

1 个答案:

答案 0 :(得分:1)

这样会更正确

$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);

?>

如果您回显isset($_POST["id"])并且确实如此,则会打印“1”而不是实际的帖子值

相关问题