Samuel Samuel - 3 years ago 131
MySQL Question

why is username registered as null in database?

I am currently working on an android app and I completed a register and login form for android.

The only Problem is that the username is always declared as a 0.

I hope you can understand me and can help me too!

MySql output

php code:
http://textuploader.com/djedy

[EDIT]

android-studio code:

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

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);
}
});
}


}

Answer Source

You are binding the username as an integer value.

mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);

My assumption is you want to bind your $age an interger value instead?

Try changing the above to this:

mysqli_stmt_bind_param($statement, "ssis", $name, $username, $age, $password);

Hope this helps!


EDIT: Hopefully you are just testing and will implement this but you NEED to be hashing your passwords!

Please look into this: http://php.net/manual/en/function.crypt.php

And start using the password_hash() and password_verify() functions as part of your login/registration scripts.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download