代码总是打印错误的用户名密码

时间:2013-01-17 06:39:32

标签: php android login

您好我正在使用以下代码检查用户名和密码是否正确以及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
?>

1 个答案:

答案 0 :(得分:1)

发布值尝试这样

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();
 }