无法从Java类向phpmyadmin输入数据

时间:2017-04-15 14:07:59

标签: java php android mysql android-volley

当我运行此代码时,我的数据库中没有新数据。我已经检查了我的连接,并使用邮递员应用程序尝试将数据输入数据库。但是当我试图将数据从我的xml文件发送到phpmyadmin时。我发送的数据没有新的数据。

当我检查userRegister.php时,它没有任何问题。

但是当我尝试提交表单时,我的数据库中没有数据。 并且toast msg出现没有任何消息只是一个空白的黑色吐司消息。

私人EditText editTextUsername,editTextEmail,editTextPassword;     私人按钮按钮注册;     private ProgressDialog progressDialog;

private TextView textViewLogin;

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


    editTextEmail = (EditText) findViewById(R.id.editTextEmail);
    editTextUsername = (EditText) findViewById(R.id.editTextUsername);
    editTextPassword = (EditText) findViewById(R.id.editTextPassword);

    textViewLogin = (TextView) findViewById(R.id.textViewLogin);

    buttonRegister = (Button) findViewById(R.id.buttonRegister);

    progressDialog = new ProgressDialog(this);

    buttonRegister.setOnClickListener(this);


}

private void registerUser() {
    final String email = editTextEmail.getText().toString().trim();
    final String username = editTextUsername.getText().toString().trim();
    final String password = editTextPassword.getText().toString().trim();

    progressDialog.setMessage("Registering user...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            Constants.URL_REGISTER,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();

                    try {
                        JSONObject jsonObject = new JSONObject(response);

                        makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.hide();
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("username", username);
            params.put("email", email);
            params.put("password", password);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);


}

@Override
public void onClick(View view) {
    if (view == buttonRegister)
        registerUser();
}

registerUser.php         

require_once'../includes/DbOperations.php';

$response = array();

if ($_SERVER['REQUEST_METHOD']=='POST'){

//check user give all the info required
if(
    isset($_POST['username'])and
        isset($_POST['email']) and
            isset($_POST['password']))

    {
//operate the data further
     //create db operation object

    $db = new DbOperations();

    //call method
    if($db-> createUser(
        $_POST['username'],
        $_POST['password'],
        $_POST['email']
        )){
        $response['error'] = false;
        $response['message'] = "User registered successfully";
    }else{
        $response['error'] = true;
        $response['message'] = "Some error occured please try again";

    }

}else{
    $response['error']= true;
    $response['message']= "Required fields are missing";

}

}else{
    $response['error'] = true;
    $response['message'] = "Invalid Request";
}


//display error in json format
echo json_encode($response);

0 个答案:

没有答案