使用PHP和MySQL在Android中登录


Login in Android using PHP and MySQL

我一直试图在MySQL中注册后创建一个登录模块,但我面临的问题是,尽管我成功地注册了值,但我无法从注册值的表中登录。这是我的login.php代码。我收到的错误消息是Invalid Username or Password,尽管它们在我的表中。

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    $username = $_POST['username'];
    $password = $_POST['password'];
    require_once('dbConnect.php');
    $sql = "SELECT * FROM register WHERE username = '$username' AND  password='$password'";
    $result = mysqli_query($con, $sql);
    $check = mysqli_fetch_array($result);
    if(isset($check)){
        echo'sucess';
    }else{
        echo'failure';
    }
    mysqli_close($con);
}
?>

这是我的Config.java文件:

public class Config {
    //URL to our login.php file
    public static final String LOGIN_URL = "http://10.0.2.2/login.php";
    //Keys for email and password as defined in our $_POST['key'] in login.php
    public static final String KEY_EMAIL = "username";
    public static final String KEY_PASSWORD = "password";
    //If server response is equal to this that means login is successful
    public static final String LOGIN_SUCCESS = "success";
    //Keys for Sharedpreferences
    //This would be the name of our shared preferences
    public static final String SHARED_PREF_NAME = "myloginapp";
    //This would be used to store the email of current logged in user
    public static final String EMAIL_SHARED_PREF = "username";
    //We will use this to store the boolean in sharedpreference to track user is loggedin or not
    public static final String LOGGEDIN_SHARED_PREF = "loggedin";
}

这是我的Login.java文件,我为创建Login:而写的文件

public class Login extends AppCompatActivity implements View.OnClickListener {
    private Button btnLogin;
    private TextView txtRegister;
    private EditText editUsername, editPassword;
    private boolean loggedIn = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        //Initializing views
        editUsername = (EditText) findViewById(R.id.editUsername);
        editPassword = (EditText) findViewById(R.id.editPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        txtRegister = (TextView) findViewById(R.id.txtRegister);
        //Adding click listener
        btnLogin.setOnClickListener(this);
        txtRegister.setOnClickListener(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        //In onresume fetching value from sharedpreference
        SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME,Context.MODE_PRIVATE);
        //Fetching the boolean value form sharedpreferences
        loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
        //If we will get true
        if(loggedIn){
            //We will start the Profile Activity
            Intent intent = new Intent(Login.this, ProfileActivity.class);
            startActivity(intent);
        }
    }
    private void login(){
        //Getting values from edit texts
        final String username = editUsername.getText().toString().trim();
        final String password = editPassword.getText().toString().trim();
        //Creating a string request
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.LOGIN_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //If we are getting success from server
                if(response.equalsIgnoreCase(Config.LOGIN_SUCCESS)){
                    //Creating a shared preference
                    SharedPreferences sharedPreferences = Login.this.getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                    //Creating editor to store values to shared preferences
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    //Adding values to editor
                    editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, true);
                    editor.putString(Config.EMAIL_SHARED_PREF,username);
                    //Saving values to editor
                    editor.commit();
                    //Starting profile activity
                    Intent intent = new Intent(Login.this, ProfileActivity.class);
                    startActivity(intent);
                }else{
                    //If the server response is not success
                    //Displaying an error message on toast
                    Toast.makeText(Login.this, "Invalid username or password", Toast.LENGTH_LONG).show();
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //You can handle error here if you want
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                //Adding parameters to request
                params.put(Config.KEY_EMAIL,username);
                params.put(Config.KEY_PASSWORD, password);
                //returning parameter
                return params;
            }
        };
        //Adding the string request to the queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnLogin:
                login();
                break;
            case R.id.txtRegister:
                Intent register = new Intent(Login.this,Register.class);
                startActivity(register);
                break;
        }
    }
}

这是我用来参考的链接:https://www.simplifiedcoding.net/android-login-example-using-php-mysql-and-volley/

您在PHP代码中拼写的"success"不正确。因此,您的应用程序将永远无法将响应与拼写正确的Config.LOGIN_SUCCESS相匹配。