0
我將一些值從android應用程序傳遞給PHP腳本。我的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腳本(它們不是在同一個文件,代碼標籤行爲不端)
你可以發佈你的PHP腳本嗎? – Divey
檢查你的情況。在你的查詢中你有'Password',但是你試圖用'$ row ['password']'從結果中提取它。 PHP數組鍵區分大小寫。 –
謝謝隊友。這解決了它。打擊我是一個奇怪的錯誤產生。這是說在劇本開始時問題是正確的。 –