为什么MySQL不存储来自Android应用程序的数据

时间:2019-04-12 07:01:44

标签: php android mysql

基本上所有其他字段(例如,用户名,密码,全名...)都可以存储到MySQL中,但不能存储在“ contact”和“ email”字段中。我在这里附了一些代码。请帮助我。

我尝试了几种方法来切换字段的顺序,但仍然得到相同的结果。我什至重新启动了xampp,但仍然无法使其正常工作。

public class RegisterActivity extends AppCompatActivity {
private static final String KEY_STATUS = "status";
private static final String KEY_MESSAGE = "message";
private static final String KEY_FULL_NAME = "full_name";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
private static final String KEY_CONTACT = "contact";
private static final String KEY_EMAIL = "email";
private static final String KEY_EMPTY = "";
private EditText etUsername;
private EditText etPassword;
private EditText etConfirmPassword;
private EditText etFullName;
private EditText etContact;
private EditText etEmail;
private String username;
private String password;
private String confirmPassword;
private String fullName;
private String contact;
private String email;
private ProgressDialog pDialog;
private String register_url = "http://10.55.1.143/buildingreg/register.php";
private SessionHandler session;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    session = new SessionHandler(getApplicationContext());
    setContentView(R.layout.activity_register);

    etUsername = findViewById(R.id.etUsername);
    etPassword = findViewById(R.id.etPassword);
    etConfirmPassword = findViewById(R.id.etConfirmPassword);
    etFullName = findViewById(R.id.etFullName);
    etContact = findViewById(R.id.etContact);
    etEmail = findViewById(R.id.etEmail);

    TextView login = findViewById(R.id.btnRegisterLogin);
    Button register = findViewById(R.id.btnRegister);

    //Launch Login screen when Login Button is clicked
    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(i);
            finish();
        }
    });

    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Retrieve the data entered in the edit texts
            username = etUsername.getText().toString().toLowerCase().trim();
            password = etPassword.getText().toString().trim();
            confirmPassword = etConfirmPassword.getText().toString().trim();
            fullName = etFullName.getText().toString().trim();
            contact = etContact.getText().toString().trim();
            email = etEmail.getText().toString().trim();
            if (validateInputs()) {
                registerUser();
            }

        }
    });

}

private void registerUser() {
    displayLoader();
    JSONObject request = new JSONObject();
    try {
        //Populate the request parameters
        request.put(KEY_USERNAME, username);
        request.put(KEY_PASSWORD, password);
        request.put(KEY_FULL_NAME, fullName);
        request.put(KEY_CONTACT, contact);
        request.put(KEY_EMAIL, email);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    JsonObjectRequest jsArrayRequest = new JsonObjectRequest
            (Request.Method.POST, register_url, request, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    pDialog.dismiss();
                    try {
                        //Check if user got registered successfully
                        if (response.getInt(KEY_STATUS) == 0) {
                            //Set the user session
                            session.loginUser(username,fullName);
                            loadMain();

                        }else if(response.getInt(KEY_STATUS) == 1){
                            //Display error message if username is already existsing
                            etUsername.setError("Username already taken!");
                            etUsername.requestFocus();

                        }else{
                            Toast.makeText(getApplicationContext(),
                                    response.getString(KEY_MESSAGE), Toast.LENGTH_SHORT).show();

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    pDialog.dismiss();

                    //Display error message whenever an error occurs
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });

    // Access the RequestQueue through your singleton class.
    MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
}

未显示错误,但数据库未显示从用户端捕获的“联系人”和“电子邮件”。

下面是注册用户的php代码。

<?php
$response = array();
include 'db/db_connect.php';
include 'functions.php';

//Get the input request parameters
$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE); //convert JSON into array

//Check for Mandatory parameters
if(isset($input['username'])&&isset($input['password'])
&&isset($input['contact'])&&isset($input['email'])
&&isset($input['full_name'])){
$username = $input['username'];
$password = $input['password'];
$fullName = $input['full_name'];
$contact = $input['contact'];
$email = $input['email'];

//Check if user already exist
if(!userExists($username)){

    //Get a unique Salt
    $salt         = getSalt();

    //Generate a unique password Hash
    $passwordHash = password_hash(concatPasswordWithSalt($password,$salt),PASSWORD_DEFAULT);

    //Query to register new user
    $insertQuery  = "INSERT INTO member(full_name, contact, email, username, password_hash, salt) VALUES (?,?,?,?)";
    if($stmt = $con->prepare($insertQuery)){
        $stmt->bind_param("ssss",$fullName,$contact,$email,$username,$passwordHash,$salt);
        $stmt->execute();
        $response["status"] = 0;
        $response["message"] = "User created";
        $stmt->close();
    }
}
else{
    $response["status"] = 1;
    $response["message"] = "User exists";
}
}
else{
$response["status"] = 2;
$response["message"] = "Missing mandatory parameters";
}
echo json_encode($response);
?>

2 个答案:

答案 0 :(得分:1)

请尝试

$insertQuery  = "INSERT INTO member(full_name, contact, email, username, password_hash, salt) VALUES (?,?,?,?,?,?)";

代替

$insertQuery  = "INSERT INTO member(full_name, contact, email, username, password_hash, salt) VALUES (?,?,?,?)";

您有6个参数,但只有4个参数。

答案 1 :(得分:0)

您的return Speed(float(self) + float(speed)) 只有6个参数的4个参数(问号)。再添加2个,就像这样:

$insertQuery

您的$insertQuery = "INSERT INTO member(full_name, contact, email, username, password_hash, salt) VALUES (?,?,?,?,?,?)"; 的掩码中也有4个参数,再添加2个bind_param()

s