2016-07-14 5 views
-3

내 안드로이드 앱의 사용자가 팀의 축구 선수를 평가할 수있는 다음 스크립트가 있습니다.PHP/mysql에서 정의되지 않은 색인 오류

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

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

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

    if($best_player){ 

     $sql_query = "insert into ratingplayerstable values('$id','$bestplayer','$rate','$user_id');"; 

     if(mysqli_query($con,$sql_query)){ 
      $_SESSION['id'] = mysqli_insert_id($con); 
      $don = array('result' =>"success","message"=>"Επιτυχής πρόσθεση παίχτη"); 
     }  
    }else if($best_player){ 
     $don = array('result' =>"fail","message"=>"Παρακαλώ συμπλήρωσε τα πεδία");    
    }  
    echo json_encode($don); 

?> 

내 서버에서 그대로 내가 그것을 실행하면, 나는 다음과 같은 다음과 같은 메시지가 얻을 : 왜

{"fail":"Παρακαλώ συμπλήρωσε τα πεδία"} 

를 얻을 내가 가진해야 데이터를 전송하지 않고

<br /> 
<b>Notice</b>: Undefined index: id in <b>/var/www/vhosts/theo- 
android.co.uk/httpdocs/ael/cms/insert_best_players.php</b> on line <b>7</b> 
<br /> 
<br /> 

<b>Notice</b>: Undefined index: User_Id in <b>/var/www/vhosts/theo- 
android.co.uk/httpdocs/ael/cms/insert_best_players.php</b> on line <b>8</b> 
<br /> 
<br /> 

<b>Notice</b>: Undefined index: player in <b>/var/www/vhosts/theo- 
android.co.uk/httpdocs/ael/cms/insert_best_players.php</b> on line <b>9</b> 
<br /> 
<br /> 
<b>Notice</b>: Undefined index: rate in <b>/var/www/vhosts/theo- 
android.co.uk/httpdocs/ael/cms/insert_best_players.php</b> on line <b>10</b> 
<br /> 
<br /> 
<b>Notice</b>: Undefined variable: don in <b>/var/www/vhosts/theo- 
android.co.uk/httpdocs/ael/cms/insert_best_players.php</b> on line <b>38</b> 
<br /> 
null 

을 이 일이 일어난거야? id, players 등의 모든 값은 내 테이블에 정의되어 있습니다. 이것은 내가 테이블 생성 방법은 다음과 같습니다

CREATE TABLE IF NOT EXISTS `ratingplayerstable ` (
`id` int(10) NOT NULL AUTO_INCREMENT, 
`player` text NOT NULL, 
`rating` text NOT NULL, 
`User_Id`int(10) NOT NULL, 

    PRIMARY KEY (`id`), 
    FOREIGN KEY (User_Id) REFERENCES user_info(User_Id) 

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=30; 

감사합니다,

테오합니다.

편집

이 내가 안드로이드에서 POST 요청을 수행하는 방법이다.

private void ratingPlayer(final String player, final String rating,final String UserId) { 
    // Tag used to cancel the request 

    //HttpsTrustManager.sssMethod(); 
    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 url 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("id", ""); 
      params.put("User_Id", UserId); 
      params.put("player", player); 
      params.put("rating", rating); 
      return params; 
     } 
    }; 
    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req); 
} 

} 

login.php 이렇게하면 오류 메시지없이 $_POST의 내용을 조회하게됩니다

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


     $user_name = $_POST['user_name']; 
     $user_pass = $_POST['user_pass']; 
     $passwordEncrypted = sha1($user_pass); 



     if($user_name && $user_pass){ 

      $sql_query = "select * from user_info where user_name ='".mysqli_real_escape_string($con, $user_name)."' and user_pass='".mysqli_real_escape_string($con, $passwordEncrypted)."' LIMIT 1";  

      $result = mysqli_query($con,$sql_query); 

      $row = mysqli_fetch_array($result); 



      if($row){ 


       $don = array('result' =>'success','message'=>'You are logged in'); 
       $_SESSION['id'] = $row['id']; 

      }else{ 

       $don = array('result' =>'fail','message'=>'User could not be found'); 
      } 
     }else if(!user_name){ 


      $don = array('result' =>"fail","message"=>"Please enter your name");    


     }else if(!$user_pass){ 

     $don = array('result' =>"fail","message"=>"Please enter your password"); 

     } 

     echo json_encode($don); 

    ?> 
+0

서버에서 실행할 때 아무도 POST 요청을하지 않으므로. –

+2

[PHP : "Notice : 정의되지 않은 변수"및 "Notice : Undefined index"] (http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index)의 가능한 복제본 – Epodax

+0

오류는 DB 테이블의 정의와 아무런 관련이 없습니다. 그것은'$ _POST'에서 찾고있는 데이터가 존재하지 않는다는 것을 의미합니다. 7 행에서'var_dump ($ _ POST);를 시도하고'$ _POST'의 내용을 확인하십시오 –

답변

1

:

$id = isset($_POST['id']) ? $_POST['id'] : false; 
    $user_id = isset($_POST['User_Id']) ? $_POST['User_Id'] : false; 
    $best_player = isset($_POST['player']) ? $_POST['player'] : false; 
    $rate = isset($_POST['rate']) ? $_POST['rate'] : false; 
+0

json 응답을 보유하는 돈 변수는 재협상되지 않습니다. ( – Theo

+0

세션 변수가 필요할 수도 있습니다. 잘 모르겠습니다. – Theo

0

먼저 벤 힐러가 절대적으로 옳다는 사용하지 마십시오 $ _POST와 같은 세계적인 virables. 둘째로 값을 보내지 않고 "즉시"스크립트를 실행하려고합니다. 테스트 목적을 위해 나는 다른 문제가 없다고 생각 = 5

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

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

    $id = isset($_POST['id'])?$_POST['id']:$_GET['id']; 
    $user_id = isset($_POST['User_Id'])?$_POST['User_Id']:$_GET['User_Id']; 
    $best_player = isset($_POST['player'])?$_POST['player']:$_GET['player']; 
    $rate = isset($_POST['rate'])?$_POST['rate']:$_GET['rate']; 

    if($best_player){ 

     $sql_query = "insert into ratingplayerstable values('$id','$bestplayer','$rate','$user_id');"; 

     if(mysqli_query($con,$sql_query)){ 
      $_SESSION['id'] = mysqli_insert_id($con); 
      $don = array('result' =>"success","message"=>"Επιτυχής πρόσθεση παίχτη"); 
     }  
    }else if($best_player){ 
     $don = array('result' =>"fail","message"=>"Παρακαλώ συμπλήρωσε τα πεδία");    
    }  
    echo json_encode($don); 

을 시도하고 get 메소드 등이 script.php? ID = 1 & USER_ID = 12 & 플레이어 = 55 & 속도를 실행하려고 POST 데이터가 제출 된 경우

0

당신은 확인해야합니다 :

if (!empty($_POST)) 
{ 
    // do stuff 
} 
0

벤의 대답은 올바른 것입니다. 일반적인 오류이기 때문에 검색해야합니다. 또한

때문에 데이터 후 사람의 'satinize 기능'을 사용하는 것은 만들 수 있습니다 : else 대신 else if($best_player)

편집의

if($best_player){ 
    // ... 
} else if($best_player){ 
    // ... 
} 

쓰기 :

또한 코드를 확인 코드 삽입 공격.

관련 문제