我正在创建一个Android应用程序,我有一个注册页面,可以将用户保存到数据库中。但由于某种原因,用户名部分继续作为整数发送(如果我输入一个字符串,它将发送'0'如果输入一个数字,它将发送正确的数字)但该值被声明为字符串???
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); //This variable here
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
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 String email = etEmail.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("Registration Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
}
catch(JSONException E){
E.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest( name,username, email,password,responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
REQUEST:
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "server link";
private Map<String, String> params;
public RegisterRequest(String name, String username, String email, 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("email", email);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
<?php
$con=mysqli_connect("localhost","username","password","dbname");
$name=$_POST["name"];
$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (name, username, email, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss",$name, $username, $email, $password );
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
我知道它可能会变得非常愚蠢,但我现在已经试图解决这个问题两天了。