简体   繁体   中英

android editText text showing up as 0 in mysql database?

I have created a simple login and register UI everything is working fine except the username editText field the text that is entered in it always shows up as 0 in the mysql database.

here is the class that uses Volley and handles the JSON response and creates a new intent that takes the user to the login screen:

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 view) {
                final String name = etName.getText().toString();
                final String username = etUserName.getText().toString();
                final String password = etPassword.getText().toString();
                final int age = Integer.parseInt(etAge.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);
            }
        });
    }
}

here is the the class that sends the request and makes the connection:

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

    public RegisterRequest(String name, String username, int age, 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("password", password);
        params.put("age", age + "");
    }

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

here is the php file used to authenticate, connect and query the database:

<?php
$con = mysqli_connect("localhost", "root", "password", "androidtest");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO users (name, username, age, 
password) VALUES (?, ?, ?, ?)");

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

$response = array();
$response["success"] = true; 

echo json_encode($response);
?>

since i am not allowed to upload attach images, here is a link to the screenshot of the database:

https://i.gyazo.com/94e1bfe5f0c75f8b81fd9406f614f2f9.png

change this in PHP

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

to

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

Username not is an integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM