Ich habe das folgende Skript, mit dem der Benutzer von meiner Android-App die Fußballspieler einer Mannschaft bewerten kann.Undefinierter Indexfehler in 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);
?>
Wenn ich es laufen, wie es von meinem Server ist, erhalte ich die folgende folgende Meldung:
<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
ohne Datenübersendung sollte ich
{"fail":"Παρακαλώ συμπλήρωσε τα πεδία"}
bekommen hatte Warum ist das passiert? Alle Werte wie ID, Player usw. sind in meiner Tabelle definiert. Dies ist, wie ich die Tabelle erstellt:
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;
Danke,
Theo.
bearbeiten
Dies ist, wie ich die Post-Anforderung aus dem Android tun.
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
<?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);
?>
Da, wenn Sie es von Ihrem Server ausführen - niemand fragt POST. –
Mögliches Duplikat von [PHP: "Hinweis: Undefinierte Variable" und "Hinweis: Undefinierter Index"] (http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Epodax
Der Fehler hat nichts mit der Definition Ihrer DB-Tabelle zu tun. Das bedeutet, dass die Daten, nach denen Sie in '$ _POST' suchen, nicht vorhanden sind. Versuchen Sie 'var_dump ($ _ POST);' in Zeile 7 und überprüfen Sie den Inhalt von '$ _POST' –