2013-01-17 100 views
-1

嗨,我正在使用以下代碼來檢查用戶名和密碼是否正確與否的PHP代碼。代碼總是打印不正確的用戶名密碼

問題是,即使我給出正確的細節,它總是給出不正確的用戶名或密碼。

String response = null; 
try { 
response = CustomHttpClient.executeHttpPost("http://192.168.3.27/abc/check.php", postParameters); //Enetr Your remote PHP,ASP, Servlet file link 
String res=response.toString(); 
// res = res.trim(); 
res= res.replaceAll("\\s+",""); 
//error.setText(res); 
error.setText(res); 
if(res.equals("1")){ 
    Intent myIntent = new Intent(LoginActivity.this, HomeActivity.class);   
startActivity(myIntent); 

} 
else 
error.setText("Sorry!! Incorrect Username or Password"); 

這是我的php代碼。

<?php 
$un=$_POST['username']; 
$pw=$_POST['password']; 
//connect to the db 
$user = ‘root’; 
$pswd = ‘root’; 
$db = ‘mylogin’; 
$conn = mysql_connect(‘localhost’, $user, $pswd); 
mysql_select_db($db, $conn); 
//run the query to search for the username and password the match 
$query = 「SELECT * FROM userpass WHERE login_id = ‘$un’ AND login_pass = ‘$pw’」; 
$result = mysql_query($query) or die(「Unable to verify user because : 」 . mysql_error()); 
//this is where the actual verification happens 
if(mysql_num_rows($result) > 0) 

echo 1; // for correct login response 
else 
echo 0; // for incorrect login response 
?> 
+0

嘗試調試;' –

+0

是否PHP代碼返回正確的響應? – cartina

+0

嘗試在php代碼中回顯1。這有助於您查找問題出在php代碼還是java代碼中。 –

回答

1

張貼值嘗試像什麼結果來了`字符串資源= response.toString()這個

nameValuePairs.add(new BasicNameValuePair("username",_username)); 
nameValuePairs.add(new BasicNameValuePair("password",_pass)); 
try 
{ 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("your url"); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
+0

我已經使用該代碼... – user1844638

+0

如果您的用戶名和密碼是正確的,只需檢查您的PHP文件編碼是否正確。特別是檢查查詢,如果都是正確的返回正確的結果,以便您可以檢查您的Android設備 –

+0

我已經發布我的PHP代碼也可以喲檢查我在做什麼錯? – user1844638

相關問題