-4

Good day! In this program, I could retrieve all the details I requested except for the username which returns to 0.

Image of the Database Result

Please see attached codes

Register.php

<?php
$con = mysqli_connect("?", "?", "?", "?");

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

$statement = mysqli_prepare($con, "INSERT INTO user (name, username, age, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);
mysqli_stmt_execute($statement);

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

echo json_encode($response); ?>

RegisterRequest.class

public class RegisterRequest extends StringRequest {

private static final String REGISTER_REQUEST_URL = "https://simonewalter.000webhostapp.com/PharmTechPH/Register.php";
private Map<String, String> params;

//ADD ORDER HERE BITCHEZ
public RegisterRequest(String name, String username, int age, String password, Response.Listener<String> listener) {
    super(Method.POST, REGISTER_REQUEST_URL, listener, null);
    params = new HashMap<>();
    params.put("name", name);
    params.put("username", username);
    params.put("age", age + "");
    params.put("password", password);
}

@Override
public Map<String, String> getParams() {
    return params;
}}

RegisterActivty.class

public class RegisterActivity extends AppCompatActivity {

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


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

    bRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String name = etName.getText().toString();
            final String username = etUsername.getText().toString();
            final int age = Integer.parseInt(etAge.getText().toString());
            final String password = etPassword.getText().toString();

            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");
                        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, age, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
            queue.add(registerRequest);
        }
    });
}}
  • I suggest you to print in a file the value of username in the php script – chiappins Feb 03 '17 at 13:44
  • 2
    **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Feb 03 '17 at 13:44
  • I never developed in android but this here does not look correct `mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);` – Masivuye Cokile Feb 03 '17 at 13:45
  • @MasivuyeCokile yes, I just answer the correction. – W4R10CK Feb 03 '17 at 13:46
  • @JayBlanchard - yes, sir. I will not use plain text for my password. I just reverted back as I cannot figure out why the username returns to 0. Thank you! – Simone Walter Feb 03 '17 at 13:53
  • @SimoneWalter, Quick suggestion try to digest password before sending to DB. Google android MessageDigest – W4R10CK Feb 03 '17 at 14:01

1 Answers1

3

The value which you are sending for username is String, and in your PHP code, you have declared int.

Change this:

mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);
                             ........^//error here

With this:

mysqli_stmt_bind_param($statement, "ssss", $name, $username, $age, $password);
W4R10CK
  • 5,502
  • 2
  • 19
  • 30