在添加功能以防止在注册过程中出现重复的电子邮件地址时遇到了很多麻烦。
我试图添加另一个SQL查询以从用户表中选择所有内容,以及一个if语句以查看此查询是否返回多行,但是我不确定如何将其合并到Java代码中。
连接到数据库的Java代码:
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_REGIST,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if (success.equals("1")) {
Toast.makeText(Register.this, "Register Success!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Register.this, Main2Activity.class));
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(Register.this, "Register Error!" + e.toString(), Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
btn_reg.setVisibility(View.VISIBLE);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Register.this, "Register Error!" + error.toString(), Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
btn_reg.setVisibility(View.VISIBLE);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", firstName);
params.put("lastName", lastName);
params.put("dateOfBirth", dob);
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
php代码:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require "conn.php";
$user_fName = $_POST["name"];
$user_lname = $_POST["lastName"];
$user_DOB = $_POST["dateOfBirth"];
$user_name = $_POST["email"];
$user_password = $_POST["password"];
$user_password = md5($user_password);
$mysqli_query = "INSERT into Patients(name, lastName, dateOfBirth, email, password) values('$user_fName', '$user_lname', '$user_DOB', '$user_name', '$user_password')";
if (mysqli_query($conn, $mysqli_query)) {
$result["success"] = "1";
$result["message"] = "success";
echo json_encode($result);
mysqli_close($conn);
} else {
$result["success"] = "0";
$result["message"] = "error";
echo json_encode($result);
mysqli_close($conn);
}
}
?>