【问题标题】:How to pass User ID from PHP to android studio如何将用户 ID 从 PHP 传递到 android studio
【发布时间】:2020-04-01 05:59:52
【问题描述】:

首先,我有一个工作用户注册和登录页面,它连接到我数据库中的 UserAccTable 表。用户将使用他们的电子邮件地址和密码登录。我需要的是在登录后获取用户 ID,然后使用另一个表的前键的意图将其传递到另一个页面。

这是我的 php 代码:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

include 'DatabaseConfigThesis.php';

$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

$email = $_POST['Email'];
$password = $_POST['Password'];

$Sql_Query = "select * from UserAccTable where Email = '$email' and Password = '$password' ";


$check = mysqli_fetch_array(mysqli_query($con,$Sql_Query));

echo "Hello, " . $check['UserID'] . " (" . $check['UserID'] . ").";

if(isset($check)){

echo "Data Matched";
}
else{
echo "Invalid Username or Password Please Try Again !";
}

}else{
echo "Check Again";
}
mysqli_close($con);

?>

我使用了这段代码 echo "Hello," 。 $check['UserID'] 。 " (" . $check['UserID'] . ").";检查它是否真的获取了实际有效的用户 ID,但我不知道如何将它传递给 android studio。我听说过使用 Sessions,但不幸的是,我不知道如何使用它。

顺便说一句,这是我在 android studio 中的登录页面代码

public class UserLog extends AppCompatActivity {

// Creating EditText.
EditText Email, Password;

// Creating button;
Button LoginButton;

// Creating Volley RequestQueue.
RequestQueue requestQueue;

// Create string variable to hold the EditText Value.
String EmailHolder, PasswordHolder;

// Creating Progress dialog.
ProgressDialog progressDialog;

// Storing server url into String variable.
String HttpUrl = "http://192.168.254.112:81/UserAccDatabaseTable/UserLogin.php";

Boolean CheckEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_log);

    // Assigning ID's to EditText.
    Email = (EditText) findViewById(R.id.etUserEmail);

    Password = (EditText) findViewById(R.id.etUserPass);

    // Assigning ID's to Button.
    LoginButton = (Button) findViewById(R.id.btnUserLogin);

    // Creating Volley newRequestQueue .
    requestQueue = Volley.newRequestQueue(UserLog.this);

    // Assigning Activity this to progress dialog.
    progressDialog = new ProgressDialog(UserLog.this);

    // Adding click listener to button.
    LoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            CheckEditTextIsEmptyOrNot();

            if (CheckEditText) {

                UserLogin();

            } else {

                Toast.makeText(UserLog.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();

            }

        }
    });

}

// Creating user login function.
public void UserLogin() {

    // Showing progress dialog at user registration time.
    progressDialog.setMessage("Please Wait");
    progressDialog.show();

    // Creating string request with post method.
    StringRequest stringRequest = new StringRequest(Request.Method.POST, HttpUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String ServerResponse) {

                    // Hiding the progress dialog after all task complete.
                    progressDialog.dismiss();

                    // Matching server responce message to our text.
                    if(ServerResponse.equalsIgnoreCase("Data Matched")) {

                        // If response matched then show the toast.
                        Toast.makeText(UserLog.this, "Logged In Successfully", Toast.LENGTH_LONG).show();

                        // Finish the current Login activity.
                        finish();

                        // Opening the user profile activity using intent.
                        Intent intent = new Intent(UserLog.this, UserChoose.class);

                        // Sending User Email to another activity using intent.
                        intent.putExtra("UserEmailTAG", EmailHolder);

                        startActivity(intent);
                    }
                    else {

                        // Showing Echo Response Message Coming From Server.
                        Toast.makeText(UserLog.this, ServerResponse, Toast.LENGTH_LONG).show();

                    }


                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                    // Hiding the progress dialog after all task complete.
                    progressDialog.dismiss();

                    // Showing error message if something goes wrong.
                    Toast.makeText(UserLog.this, volleyError.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() {

            // Creating Map String Params.
            Map<String, String> params = new HashMap<String, String>();

            // Adding All values to Params.
            // The firs argument should be same sa your MySQL database table columns.
            params.put("Email", EmailHolder);
            params.put("Password", PasswordHolder);

            return params;
        }

    };

    // Creating RequestQueue.
    RequestQueue requestQueue = Volley.newRequestQueue(UserLog.this);

    // Adding the StringRequest object into requestQueue.
    requestQueue.add(stringRequest);

}


public void CheckEditTextIsEmptyOrNot() {

    // Getting values from EditText.
    EmailHolder = Email.getText().toString().trim();
    PasswordHolder = Password.getText().toString().trim();

    // Checking whether EditText value is empty or not.
    if (TextUtils.isEmpty(EmailHolder) || TextUtils.isEmpty(PasswordHolder)) {

        // If any of EditText is empty then set variable value as False.
        CheckEditText = false;

    } else {

        // If any of EditText is filled then set variable value as True.
        CheckEditText = true;
    }
}
}

我希望有人可以帮助我。我知道我的 php 代码容易发生 SQL 注入,我会在完成此问题后修复它。谢谢

【问题讨论】:

标签: php android mysql


【解决方案1】:

idk 你将如何管理 php 请求。只需使用本教程 mkay? https://www.tutorialspoint.com/android/android_php_mysql.htm

基本上你在php中echo,你会得到的

@Override
protected void onPostExecute(String result){
    //idk show it maybe?
    Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
}

【讨论】:

  • 这是一个糟糕的教程!不要使用这个教程。
【解决方案2】:

好的,所以我尝试修复我的查询,现在是这样的:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

include 'DatabaseConfigThesis.php';

$con = mysqli_connect($HostName, $HostUser, $HostPass, $DatabaseName);

$Email = mysqli_real_escape_string($con, $_POST["Email"]);
$Password = mysqli_real_escape_string($con, $_POST["Password"]);

$SQL = $con->prepare("SELECT * from UserAccTable where Email= ? AND Password= ? ");
$SQL->bind_param('s', $Email, $Password); // 's' specifies the variable type => 'string'
$SQL->execute();

$check = mysqli_fetch_array(mysqli_query($con,$SQL));

 mysqli_close($con);
?>

我得到了 mysqli_stmt::bind_param();类型定义字符串中的元素数量与绑定变量的数量不匹配。我不知道是它不接受密码还是因为其他问题。

【讨论】:

    猜你喜欢
    • 2017-02-12
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多