2013-07-10 39 views
0

我写了PHP代码以从Database中获取数据。它获得登录值。 (用户名为&密码)。我通过mysql server/localhost连接它。当我运行这个PHP代码时,它总是显示“0”。这意味着它不会从数据库获取数据。这是为什么?在Android中使用PHP登录表单

这里我的PHP代码:

<?php 
$hostname_localhost ="localhost"; 
$database_localhost ="gpsvts_geotrack"; 
$username_localhost ="root"; 
$password_localhost =""; 
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost) 
or 
trigger_error(mysql_error(),E_USER_ERROR); 

mysql_select_db($database_localhost, $localhost); 

$username = $_POST['uname']; 
$password = $_POST['passwd']; 
$query_search = "select 'uname' & 'passwd' from user_master where uname = '.$username.' AND passwd = '.$password.'"; 
$query_exec = mysql_query($query_search) or die(mysql_error()); 
$rows = mysql_num_rows($query_exec); 
//echo $rows; 
if($rows == 0) { 
echo "No Such User Found"; 
} 
else { 
    echo "User Found"; 
} 
?> 

我把这个在我的WAMP服务器的WWW folder.when我运行这个PHP文件WAMP的服务器本地主机总是说“没有发现这样的用户”。

我用这个php文件从数据库中获取数据来连接android登录表单。它包含两个字段。这是用户名为&的密码。

这里我给我的android登录代码。

b = (Button)findViewById(R.id.Button01); 
    et = (EditText)findViewById(R.id.username); 
    pass= (EditText)findViewById(R.id.password); 
    tv = (TextView)findViewById(R.id.tv); 

    b.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog = ProgressDialog.show(AndroidPHPConnectionDemo.this, "", 
        "Validating user...", true); 
      new Thread(new Runnable() { 
        public void run() { 
         login();       
        } 
        }).start();    
     } 
    }); 
} 

void login(){ 
    try{    

     httpclient=new DefaultHttpClient(); 
     httppost= new HttpPost("http://10.0.2.2//new/nuwan1.php"); // make sure the url is correct. 
     //add your data 
     nameValuePairs = new ArrayList<NameValuePair>(2); 
     // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar, 
     nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value']; 
     nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     //Execute HTTP Post Request 
     response=httpclient.execute(httppost); 
     // edited by James from coderzheaven.. from here.... 
     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     final String response = httpclient.execute(httppost, responseHandler); 
     System.out.println("Response : " + response); 
     runOnUiThread(new Runnable() { 
      public void run() { 
       tv.setText("Response from PHP : " + response); 
       dialog.dismiss(); 
      } 
     }); 

     if(response.equalsIgnoreCase("User Found")){ 
      runOnUiThread(new Runnable() { 
       public void run() { 
        Toast.makeText(AndroidPHPConnectionDemo.this,"Login Success", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

      startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class)); 
     }else{ 
      showAlert();     
     } 

    }catch(Exception e){ 
     dialog.dismiss(); 
     System.out.println("Exception : " + e.getMessage()); 
    } 
} 
public void showAlert(){ 
    AndroidPHPConnectionDemo.this.runOnUiThread(new Runnable() { 
     public void run() { 
      AlertDialog.Builder builder = new AlertDialog.Builder(AndroidPHPConnectionDemo.this); 
      builder.setTitle("Login Error."); 
      builder.setMessage("User not Found.") 
        .setCancelable(false) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
         } 
        });      
      AlertDialog alert = builder.create(); 
      alert.show();    
     } 
    }); 
} 

总是我没有这样的用户发现& PHP响应alart是没有这样的用户发现

这是为什么? 请帮助我。

我用下面的PHP代码

<?php 
$un=$_POST['uname']; 
$pw=$_POST['passwd']; 
//connect to the db 

$host="localhost"; // Host name 
$user="root"; // Mysql username 
$pswd=""; // Mysql password 
$db="gpsvts_geotrack"; // Database name 
$tbl_name="user_master"; // Table name 

$conn = mysql_connect($host, $user, $pswd); 
mysql_select_db($db, $conn); 
//run the query to search for the username and password the match 
$query = "SELECT * FROM $tbl_name WHERE uname = '$un' AND passwd= '$pw'"; 
//$query = "SELECT uid FROM $tbl_name WHERE uname = '$un' AND passwd = '$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 mysql_result($result,0); // for correct login response 
else 
echo 0; // for incorrect login response 
?> 

然后返回uid作为响应。但不验证用户。有PHP代码错误或Android代码错误。我想匹配的值用户输入&数据库得到。这是否发生在这里。 如果不给我正确的事情。

+0

如果这回答了你的问题,请把它标记为接受的答案:) – Nick

+0

尼克的回答将致力于改变“密码”“psswd”从你的PHP和Android需要匹配每一个参数。 –

回答

4

在程序中你是从你身边掠过的是:

nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value']; 
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 

,而您试图从PHP端获取的是:

$un=$_POST['uname']; 
$pw=$_POST['passwd']; 

所以更改nameValuePairs中的名称与此一起传递:

nameValuePairs.add(new BasicNameValuePair("uname",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value']; 
nameValuePairs.add(new BasicNameValuePair("passwd",pass.getText().toString().trim())); 
+0

这就是我说的同样的事情.... – Nick

1

如果我没有弄错它应该是这样的,因为你的帖子变量拼写与你在名称值对中的拼写不同。

nameValuePairs.add(new BasicNameValuePair("uname",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value']; 
nameValuePairs.add(new BasicNameValuePair("passwd",pass.getText().toString().trim()));