Click here to Skip to main content
14,880,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to register a user in the registration page but getting error, i have converted every details i need to string but still not working,maybe the problem is from the php code.the insert doesn't work.

What I have tried:

this is the Registration activity
public class Register extends AppCompatActivity {

    EditText name1, etpolicyNumber, etPassword;
    Button bRegister;


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


        name1 = (EditText) findViewById(R.id.name);
        etpolicyNumber = (EditText) findViewById(R.id.policy_number1);
        etPassword = (EditText) findViewById(R.id.password1);
        bRegister = (Button) findViewById(R.id.btnregister);

        bRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = name1.getText().toString();
                String policyNumber = etpolicyNumber.getText().toString();
                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(Register.this, MainActivity.class);
                                startActivity(intent);
                                Toast.makeText(Register.this, "Registration Successful", Toast.LENGTH_SHORT).show();
                            } 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(name, policyNumber, password, responseListener);
                RequestQueue queue = Volley.newRequestQueue(Register.this);
                queue.add(registerRequest);
            }
        });

    }
}


this is the RegisterRequest java

public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://beigecare.ga/Register.php";
    private Map<String, String> params;

    public RegisterRequest(String name, String policyNumber, String password, Response.Listener<String> listener){
        super(Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("policyNumber", policyNumber);
        params.put("password", password);


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


this is the register php code
<?php
    $con = mysqli_connect("mysql.hostinger.in", "u608965245_user", "Engmann1", "u608965245_data");
    
    $name = $_POST["name"];   
    $policynumber = $_POST["policyNumber"];
    $password = $_POST["password"];
    $statement = mysqli_prepare($con, "INSERT INTO user (name, policynumber, password) VALUES (?, ?, ?)");
    mysqli_stmt_bind_param($statement, "siss", $name, $policynumber, $password);
    mysqli_stmt_execute($statement);
    
    $response = array();
    $response["success"] = true;  
    
    
    echo json_encode($response);
?>

Please help me correctn it
Posted
Updated 24-Sep-19 0:31am
Comments
Richard MacCutchan 16-Jun-17 12:43pm
   
Where is the error, and what is it?
Member 13053943 19-Jun-17 8:20am
   
i solved the problem,it was the php file,i was making a int a string.i changed it to a string

mysqli_stmt_bind_param($statement, "siss", $name, $policynumber, $password);

i changed the siss to sss
Member 13053943 19-Jun-17 14:45pm
   
ok bro,i will follow up

mysqli_stmt_bind_param($statement, "siss", $name, $policynumber, $password);

i changed the siss to sss
   
public class Registration extends AppCompatActivity {
TextView lg;
EditText name, address, station, state, phone, type, email, password;
Button btn;
private static final String KEY_EMPTY = "";
private String register_url = "http://192.168.101.10/mandisoftphp/index.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
name = (EditText) findViewById(R.id.name);
address = (EditText) findViewById(R.id.address);
station = (EditText) findViewById(R.id.station);
state = (EditText) findViewById(R.id.state);
phone = (EditText) findViewById(R.id.phone);
type = (EditText) findViewById(R.id.type);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View view) {
String namestring = name.getText().toString().trim();
String addressstring = address.getText().toString().trim();
String stationstring = station.getText().toString().trim();
String statestring = state.getText().toString().trim();
String phoneNumber = phone.getText().toString().trim();
String typestring = type.getText().toString().trim();
String emailstring = email.getText().toString().trim();
String passwordstring = password.getText().toString().trim();

if (validateInputs()) {
register();
}
}
});

}

private boolean validateInputs ()
{
if (KEY_EMPTY.equals(name)) {
name.setError("Full Name cannot be empty");
name.requestFocus();
return false;
}
if (KEY_EMPTY.equals(address)) {
address.setError("Full Name cannot be empty");
address.requestFocus();
return false;
}
if (KEY_EMPTY.equals(station)) {
station.setError("Full Name cannot be empty");
station.requestFocus();
return false;
}
if (KEY_EMPTY.equals(state)) {
state.setError("Full Name cannot be empty");
state.requestFocus();
return false;
}

if (KEY_EMPTY.equals(phone)) {
phone.setError("Full Name cannot be empty");
phone.requestFocus();
return false;
}
if (KEY_EMPTY.equals(type)) {
type.setError("Full Name cannot be empty");
type.requestFocus();
return false;
}
if (KEY_EMPTY.equals(email)) {
email.setError("Full Name cannot be empty");
email.requestFocus();
return false;
}
if (KEY_EMPTY.equals(password)) {
password.setError("Full Name cannot be empty");
password.requestFocus();
return false;
}
return false;
}
private void register() {

btn.setVisibility(View.GONE);
final String name = this.name.getText().toString().trim();
final String address = this.address.getText().toString().trim();
final String station = this.station.getText().toString().trim();
final String state = this.state.getText().toString().trim();
final String phone = this.phone.getText().toString().trim();
final String email = this.email.getText().toString().trim();
final String password = this.password.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, register_url,
new Response.Listener<string>() {
@Override
public void onResponse(String response) {

try {

JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if (success.equals("1")) {
Toast.makeText(Registration.this, "insert success", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(Registration.this, "insert error" + e.toString(), Toast.LENGTH_SHORT).show();

}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Registration.this, "insert error" + error.toString(), Toast.LENGTH_SHORT).show();

btn.setVisibility(View.VISIBLE);
}
}) {
protected Map<string, string=""> getParams() throws AuthFailureError {
Map<string, string=""> params = new HashMap<>();
params.put("name",name);
params.put("address",address);
params.put("station",station);
params.put("state",state);
params.put("phone",phone);
params.put("email",email);
params.put("password",password);
return super.getParams();
}
};

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
   
Comments
Member 14602203 24-Sep-19 6:38am
   
any question and few time question ans slove
CHill60 24-Sep-19 7:54am
   
An unformatted, unexplained code dump is not a solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900