【问题标题】:how to edit the values in Mysql database using php in android studio如何在 android studio 中使用 php 编辑 Mysql 数据库中的值
【发布时间】:2016-07-04 08:00:48
【问题描述】:

我想从 android studio 编辑数据库值。我的表由 ("id","name","username","email","age","password" 组成,这是我的 php 代码,它将信息上传到数据库,但我想如何更改密码我可以编辑我的代码,以便删除“密码”列中的特定数据并用新的“密码”替换。

我的 php 代码:

$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $id, $name, $username, $email, $age, $password);

$response = array();
$response["success"] = false;  

while(mysqli_stmt_fetch($statement)){
    $response["success"] = true;  
    $response["name"] = $name;
    $response["username"] = $username;
    $response["email"]= $email;
    $response["age"] = $age;    
    $response["password"] = $password;

}

echo json_encode($response);
?>

这是我的 MainActivity 代码:

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

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

    final EditText etAge = (EditText) findViewById(R.id.etAge);
    final EditText etName = (EditText) findViewById(R.id.etName);
    final EditText etUsername = (EditText) findViewById(R.id.etUsername);
    final EditText etPassword = (EditText) findViewById(R.id.etPassword);
    final Button bRegister = (Button) findViewById(R.id.bRegister);
    final EditText etEmail=(EditText) findViewById(R.id.etEmail);

    assert bRegister != null;
    assert bRegister != null;
    bRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            assert etName != null;
            final String name = etName.getText().toString();
            assert etUsername != null;
            final String username = etUsername.getText().toString();
            assert etAge != null;
            final String age = etAge.getText().toString();
            assert etPassword != null;
            final String password = etPassword.getText().toString();
            assert etEmail != null;
            final String email = etEmail.getText().toString();

            Response.Listener<String> responseListener = new Response.Listener<String>() {
                private ProgressDialog loading;
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");
                        loading = ProgressDialog.show(RegisterActivity.this, "Please wait...", "Registering...", false, false);
                        if (success) {
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

            RegisterRequest registerRequest = new RegisterRequest(name, username, email, age, password, responseListener);
            RequestQueue queue =    Volley.newRequestQueue(RegisterActivity.this);
            queue.add(registerRequest);
        }
    });
}

【问题讨论】:

  • 创建一个新的 PHP 文件并设置 sql 语句 UPDATE user SET Password = Whateveryournewpasswordis where username=yourusername 并在 Android 中创建一个活动并通过 HashMaps 和 JSonObjectRequest 传递新密码(不一定)。这是较短的版本。遵循您之前使用的 SQL 注入预防 :)

标签: php android mysql database


【解决方案1】:

创建一个新的 php 文件并执行以下操作:

if( !isset($_POST['username'], $_POST['password'], $_POST['newPassword']) ) {
    throw new \InvalidArgumentException( "Incorrect data" );
}    

// Check new password length
if( strlen($_POST['newPassword']) < 6 ) {
    // Throw?
}

$username = $_POST["username"];
$password = $_POST["password"];
$newPassword = $_POST["newPassword"];

// You should probably do a select here and check if the user exist and return a error message if not (or you can check the affected rows). 
// Also keep in mind that this enables people to bruteforce your accounts.
$statement = mysqli_prepare($con, "UPDATE user SET password = ? WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "sss", $newPassword, $username, $password);
mysqli_stmt_execute($statement);

【讨论】:

    猜你喜欢
    • 2017-02-28
    • 1970-01-01
    • 2012-10-31
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多