我正在将一些值从 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 脚本(它们不在同一个文件中,代码标签行为不端)