1

I am trying to understand how to save user details on Server Database. Although by watching a video and following the steps as mentioned, I am able to save name, password, dob but email field shows 0 everytime. Please tell me where the issue is, I am not able to understand. Below is my Code.

Register.php

<?php
$con = mysqli_connect("my_host", "db_user", "db_password", "my_db");
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$dob = $_POST["dob"];
$statement = mysqli_prepare($con, "INSERT INTO user (name, email, password, dob) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $email, $password, $dob);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;  
echo json_encode($response);
?>

I am using Volley for this.

RegisterRequest.class

public class RegisterRequest extends StringRequest {

private static final String REQUEST_REGISTER_URL = "http://www.myhostingaddress/register.php";
private Map<String, String> params;

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

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

and my Main Registration Activity

Register.class

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

    name = (EditText) findViewById(R.id.name);
    eemail = (EditText) findViewById(R.id.email);
    password = (EditText) findViewById(R.id.password_signup);
    dob = (EditText) findViewById(R.id.DOB);
    register_btn = (Button) findViewById(R.id.Signup);

    register_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String Name = name.getText().toString();
            final String Email = eemail.getText().toString();
            final String Password = password.getText().toString();
            final String Dob = dob.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(Sign_up.this, "Account Created, Login now !", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(Sign_up.this,Login_Page.class));
                        } else {
                            Toast.makeText(Sign_up.this, "Registration Failed, Try Again !", Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };

            RegisterRequest registerRequest = new RegisterRequest(Name,Email,Password,Dob,responseListener);
            RequestQueue queue = Volley.newRequestQueue(Sign_up.this);
            queue.add(registerRequest);
        }
    });
}
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ravi Bhardwaj
  • 128
  • 1
  • 15
  • 1
    Take a good look at your second column and the second argument you passed in the bind. – Funk Forty Niner Sep 20 '17 at 16:17
  • **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). ***It is not necessary to [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 Sep 20 '17 at 16:21
  • Thanks for your help. Issue is solved now. – Ravi Bhardwaj Sep 20 '17 at 18:53

1 Answers1

0

The answer here is simple, you used an i (integer) instead of an s (string) for the email column in the bind_param()'s argument. I sincerely doubt that that is an integer.

You also best make sure that the email column's type is indeed a type that can store a string.

Therefore, change siss to ssss for all strings.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141