1

I want to edit the database values from android studio. My table consists of ("id","name","username","email","age","password", here is my php code which will upload the info to database, but i want to change the password how can i edit my code so that the particular data in "password" column is erased and replaced with the new "password".

My php code:

$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);
?>

and here is my MainActivity code:

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);
        }
    });
}
1
  • Create a new PHP file and set the sql statment UPDATE user SET Password = Whateveryournewpasswordis where username=yourusername and in the Android create an activity and pass the new password via HashMaps and JSonObjectRequest(not necessarily). This is the shorter version. Follow the SQL injection prevention you have used earlier :) Commented Jul 4, 2016 at 8:12

1 Answer 1

1

Create a new php file and do something like:

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.