我学习 android 并尝试使用 PHP 脚本和 WAMP 服务器编写一些代码来验证用户名和密码。我不断从我的 PHP 脚本中收到未定义的索引错误。据我所知,这意味着我的 PHP 脚本无法从 URL 访问数据。这是相关的代码。任何帮助表示赞赏。
//build url data to be sent to server
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
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));
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");
$username = $_POST["username"];
$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 脚本给出了这些错误,$username 和 $password 变量也包含我的 android 应用程序试图传递的值。然而,这些错误的存在仍然困扰着我的代码,因为错误表的 HTML 在响应中被发送回 android 应用程序