0

我已经编写PHP了代码来从中获取数据Database。它获取登录值。(用户名密码)。我通过mysql server/localhost. 当我运行这个 PHP 代码时,它总是显示“0”。这意味着它没有从数据库中获取数据。这是为什么?

这是我的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 文件夹中。当我运行这个 php 文件 wamp 服务器本地主机时,它总是说“没有找到这样的用户”。

我使用这个 php 文件从 db 获取数据以连接 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 响应是没有找到这样的用户

这是为什么?请帮助我。

我使用了以下 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代码错误。我想匹配用户输入和数据库获取的值。是不是在这里发生。如果不给我正确的东西。

4

2 回答 2

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())); 
于 2013-07-10T04:54:46.363 回答
1

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

nameValuePairs.add(new BasicNameValuePair("uname",et.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("passwd",pass.getText().toString().trim())); 
于 2013-07-10T01:33:01.080 回答