Okay, so I'm trying to make a feature that allows people to register accounts on my app. I'm using android studio with volley. PHP is handling the server side. Code is below:
Java:
RegisterRequest.java
package james.gcsecomputingapp;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://placeholder.net/register2.php";
private Map<String, String> params;
public RegisterRequest(String first_name, String last_name, String email, String username, String password, Response.Listener<String> listener){
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("first_name", first_name);
params.put("last_name", last_name);
params.put("email", email);
params.put("username", username);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
Register.java
package james.gcsecomputingapp;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
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 register extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etFirstName = (EditText) findViewById(R.id.etFirstName);
final EditText etLastName = (EditText) findViewById(R.id.etLastName);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
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 view) {
final String firstName = etFirstName.getText().toString();
final String lastName = etLastName.getText().toString();
final String email = etEmail.getText().toString();
final String username = etUsername.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){
Toast.makeText(register.this, "Work", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(register.this, login.class);
startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(register.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(firstName,lastName,email,username,password, responseListener);
RequestQueue queue = Volley.newRequestQueue(register.this);
queue.add(registerRequest);
}
});
}
}
PHP:
Register.php
<?php
$con=mysqli_connect("localhost","user","pass","db_database");
$first_namename = $_POST["first_name"];
$last_name = $_POST["last_name"];
$email = $_POST["email"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO `db_database`.`users` (`user_id`, `first_name`, `last_name`, `email`, `username`, `password`) VALUES (NULL, first_name, last_name, email, username, password)");
mysqli_stmt_bind_param($statement, "siss",$first_name,$last_name,$email,$username,$password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
This code seemingly produces no errors, but obviously it does not work properly. When the code is run it produces an entry in the SQL database, as expected. But those entries contain no data? Don't understand what I mean by that? Sorry about that, here's a screenshot of the table after the code has been run twice:
Any advice on this? Anyone know what is going wrong? As I say, the code 'works' as in it runs, but it doesn't do what it is intended to do.
I'm fairly new to this so if anyone could be nice and give me a hand I'd really appreciate it. Cheers!
Edit - I'm aware that there is no form of encryption and passwords are stored in plain text. I know it should never happen, but its a thing for this assignment I have. It's complicatied, lets just pretend it's just another string.