PHP:未定义的索引错误,但变量在打印时具有正确的值

时间:2013-08-21 03:31:09

标签: php android

我将一些值从Android应用程序传递给PHP脚本。我在PHP脚本中得到一个未定义的索引错误,但是当我从脚本中打印出来时,变量具有正确的值。我希望这些错误消失,但我无法弄清楚为什么他们首先在那里。以下是将它们传递给PHP脚本的方法。

Java代码

//build url data to be sent to server
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",username));
nameValuePairs.add(new BasicNameValuePair("password",password));

String result = "";
InputStream is = null;
//http post
try{
  HttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost("http://10.0.2.2/PasswordCheck.php");
  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
  HttpResponse response = httpclient.execute(httppost);
  HttpEntity entity = response.getEntity();
  is = entity.getContent();
}catch(Exception e){
  Log.e("Connection", "Error in http connection "+e.toString());
}

PHP代码:

<?php
mysql_connect("localhost", "root", "") or die("could not connect to mysql");
mysql_select_db("drop-in") or die("database not found");

if(isset($_POST["username"])){
    $username = $_POST["username"];
}
if(isset($_POST["password"])){
    $suppliedPassword = $_POST["password"];
}

$databasePassword = "";
$output = "false";
$query = mysql_query("SELECT Password FROM users WHERE Username = '$username'") or die("query failed");

if(mysql_num_rows($query) > 0){
    $row = mysql_fetch_assoc($query); 
    $databasePassword = $row['password'];
    if($databasePassword == $suppliedPassword)
    {
        $output = "true";
    }
}
    print($output);
    mysql_close();

?>

编辑:添加了PHP脚本(它们不在同一个文件中,代码标记行为不正常)

1 个答案:

答案 0 :(得分:0)

原来这是一个简单的拼写错误。我在循环中使用了'password'一词中的小写'p'而不是大写字母。奇怪的是,它造成了错误。