数据库无法获取发送给它的信息

时间:2016-09-10 12:13:45

标签: php android mysql

我一直试图找到问题几个小时,但我似乎无法找到它。 我的代码从注册表中的字段获取信息,然后使用volley将其发送到Register.php。 Register.php文件应该将数据插入数据库,但由于某种原因它不会发生
源代码:

Register.php:

<?php
$con = mysqli_connect("http://shay.oversight-group.net/", "******", "*****", "shay_shay");


$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con,"INSERT INTO user (name, age, username, password) VALUES (?,?,?,?)");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);

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

echo json_encode $response; ?>

android studio java code:

    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());

                final Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) { //When response is coming back from Register.php
                        try {
                            JSONObject jsonResponse = new JSONObject(response); //New json object getting the response of register.php
                            boolean success = jsonResponse.getBoolean("success");
                            if(success == true){
                                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); // moving to login activity if register was successful
                                startActivity(intent);
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this); //shows an error if register failed.
                                builder.setMessage("Register Failed").setNegativeButton("Retry", null).create().show(); //created the error alert
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                RegisterRequest registerRequest = new RegisterRequest(name, username, password, age, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });
    }
}



public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL = "http://shay.oversight-group.net/Register.php";
    private Map<String, String> params;

    public RegisterRequest(String name, String username, String password, int age, 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() {
        System.out.println(params);
        return params;
    }
}

0 个答案:

没有答案