gpt4 book ai didi

android - USER尚未插入数据库,即使得到正确的响应

转载 作者:行者123 更新时间:2023-11-29 09:24:13 24 4
gpt4 key购买 nike

当我尝试使用android时,用户未添加到数据库中,但可以使用POSTMAN。
POST请求,x-www-form-urlencoded
和参数传递的用户名,密码,电子邮件
我不知道为什么会这样,我得到的响应是错误:错误和消息“用户注册成功”,但仍未插入MySQL数据库

JAVA:注册

private void registerUser(final String name, final String email, final String password) {
String tag_string_req = "req_register";

StringRequest strReq = new StringRequest(Request.Method.POST,
Functions.REGISTER_URL, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response);

try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
Functions logout = new Functions();
logout.logoutUser(getApplicationContext());

Bundle b = new Bundle();
b.putString("email", email);
Intent i = new Intent(RegisterActivity.this, EmailVerify.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtras(b);
startActivity(i);
finish();

} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("message");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage(), error);
}
}) {

@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
MyApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
}

}


PHP文件:

    public function createUser($username, $pass, $email){
if($this->isUserExist($username,$email)){
return 0;
}else{

$verified=0;
$otp = rand(100000, 999999);
$password = md5($pass);
$stmt = $this->con->prepare("INSERT INTO `users` (`id`, `username`, `password`, `email`,`otp`,`verified`) VALUES (NULL, ?, ?, ? , ? , ?);");
$stmt->bind_param("sssdd",$username,$password,$email,$otp,$verified);

if($stmt->execute()){
return 1;
}else{
return 2;
}
}
}


$result = $db->createUser( $_POST['username'],$_POST['password'],$_POST['email']);
if($result == 1){
$response['error'] = false;
$response['message'] = "User registered successfully";
}

最佳答案

我曾经遇到此错误,这是由于JSON Volley的cachable属性导致,只需尝试添加以下行:

strReq.setShouldCache(false);

在此MyApplication.java => MyApplication.getInstance()。addToRequestQueue(strReq,tag_string_req);中添加此行。
RequestQueue.getCache()。clear();

供参考:
https://www.androidpit.com/forum/785063/disable-the-cache-in-the-volley-library

这也可能是由于未关闭php中的stmts和连接

关于android - USER尚未插入数据库,即使得到正确的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59881640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com