簡體   English   中英

嘗試將在Android中捕獲的用戶名和密碼傳遞給PHP以查詢MYSQL數據庫

[英]Try to pass the username and password captured in Android to PHP to query MYSQL database

該代碼查詢用戶表(“名稱”)以檢索與用戶輸入的用戶名(唯一)相對應的中文名稱。

在調試模式下,發現$ _POST ['username']中的用戶名傳遞給PHP時為空。 我可以在DEBUG模式下檢索中文名稱。 請幫助找出參數傳遞中的錯誤。

Android部分向PHP發送POST請求。

public class SigninActivity  extends AsyncTask<String,Void,String> {

private TextView statusField,roleField;
private Context context;
private int byGetOrPost = 0; 
//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,TextView statusField,
TextView roleField,int flag) {
  this.context = context;
  this.statusField = statusField;
  this.roleField = roleField;
  byGetOrPost = flag;
}

protected void onPreExecute(){

}
@Override
protected String doInBackground(String... arg0) {
  if(byGetOrPost == 0){ //means by Get Method
     try{
        String username = (String)arg0[0];
        String password = (String)arg0[1];
        String link = "http://www.example.com/login.php?username="
        +username+"&password="+password;
        URL url = new URL(link);
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(link));
        HttpResponse response = client.execute(request);
        BufferedReader in = new BufferedReader
       (new InputStreamReader(response.getEntity().getContent()));

       StringBuffer sb = new StringBuffer("");
       String line="";
       while ((line = in.readLine()) != null) {
          sb.append(line);
          break;
        }
        in.close();
        return sb.toString();
  }catch(Exception e){
     return new String("Exception: " + e.getMessage());
  }
  }
  else{
     try{
        String username = (String)arg0[0];
        String password = (String)arg0[1];
        String link="http://www.example.com/login.php";
        String data  = URLEncoder.encode("username", "UTF-8") 
        + "=" + URLEncoder.encode(username, "UTF-8");
        data += "&" + URLEncoder.encode("password", "UTF-8") 
        + "=" + URLEncoder.encode(password, "UTF-8");
        URL url = new URL(link);
        URLConnection conn = url.openConnection(); 
        conn.setDoOutput(true); 
        OutputStreamWriter wr = new OutputStreamWriter
        (conn.getOutputStream()); 
        wr.write( data ); 
        wr.flush(); 
        BufferedReader reader = new BufferedReader
        (new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        // Read Server Response
        while((line = reader.readLine()) != null)
        {
           sb.append(line);
           break;
        }
        return sb.toString();
     }catch(Exception e){
        return new String("Exception: " + e.getMessage());
     }
  }
   }
@Override
protected void onPostExecute(String result){
  this.statusField.setText("Login Successful");
  this.roleField.setText(result);
   }
  }


// Code in PHP

$con = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password) or     die("Could not connect database");



if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
} 


mysqli_select_db($con, $mysql_database) or die("Could not select database");


if (DEBUG) {
    echo "Connected to database $mysql_database";
}


mysqli_set_charset($con, 'utf8');

$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);

if (DEBUG) {
    $username = "b";
} 

$qry1 = "SELECT c_f_name FROM name where username = \"$username\" ";

$result = mysqli_query($con, $qry1);
if(DEBUG) {
echo "This is the username: ";
echo $username;

if ($result) {
    echo $username;
//successful query
if (mysqli_num_rows($result) > 0) {
    echo "query successful - 
    ";
    echo mysqli_num_rows($result);
 } else {
    echo "query successful-0 rows
    ";
}
} else {
// query not successful
echo "Query unsuccessful";
}

}

$row = mysqli_fetch_array($result);
//Since there is only 1 record that match, just retrieve the first and only record
$data = $row[0];
if($data){
echo $data;
}

mysqli_close($con);
?>

您正在通過網址傳遞用戶名/密碼

http://www.example.com/login.php?username="+username+"&password="+password

因此,請使用$ _GET ['username']或$ _REQUEST ['username']來請求您的值。

您正在傳遞GET變量並嘗試從POST方法中檢索變量。.該方法不起作用,您還必須使用$GET['variable_name']$REQUEST['variable_name']從該url檢索數據。

這是一條簡單的規則,即如果要通過get或post方法傳遞數據,則必須以相同的方法檢索該數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM