我编码为注册代码,但它不会注册,我正在将Android Studio与phpmysql一起使用。当我单击注册按钮时,它不会注册,也无法保存在本地主机的数据库中。我的代码已经为数据库设置了,但是我不明白注册按钮没有执行任何操作的问题。我目前也正在使用凌空将数据从android studio传递到phpmysql数据库。
地主注册类。
public class Landlordregistration extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landlordregistration);
final EditText Name = (EditText)findViewById(R.id.lanname);
final EditText Username = (EditText)findViewById(R.id.lanusername);
final EditText Password = (EditText)findViewById(R.id.password);
final EditText Phone = (EditText)findViewById(R.id.phone);
final EditText Address = (EditText)findViewById(R.id.lanaddress);
final Button Landlordregister = (Button)findViewById(R.id.landlordregister);
Landlordregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String lanName = Name.getText().toString();
final String lanUsername = Username.getText().toString();
final String password = Password.getText().toString();
final String phone = Phone.getText().toString();
final String address = Address.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(Landlordregistration.this, Loginlandlord.class);
Toast toast = Toast.makeText(getApplicationContext(), "Successfully Registered", Toast.LENGTH_LONG);
toast.show();
Landlordregistration.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(Landlordregistration.this);
builder.setMessage("Registration failed")
.setNegativeButton("Try Again", null)
.create()
.show();
}
}
catch (JSONException e){
e.printStackTrace();
}
}
};
LandlordregistrationRequest landlordregistrationRequest = new LandlordregistrationRequest(lanName, lanUsername, password, phone, address, responseListener);
RequestQueue queue = Volley.newRequestQueue(Landlordregistration.this);
queue.add(landlordregistrationRequest);
}
});
}
}
房东注册要求类
public class LandlordregistrationRequest extends StringRequest {
private static String URL_REGIST="http://127.0.0.1/script/registerLandlordtest.php";
private Map<String, String> params;
public LandlordregistrationRequest(String lanName, String lanUsername, String password, String phone, String address, Response.Listener<String> listener ){
super(Method.POST, URL_REGIST, listener, null);
params = new HashMap<>();
params.put("lanName", lanName);
params.put("lanUsername", lanUsername);
params.put("password", password);
params.put("phone", phone);
params.put("address", address);
}
@Override
public Map<String, String> getParams(){
return params;
}
}
registerLandlord.php
<?php
require "init.php";
$connect = mysqli_connect("localhost","root","","mytenant");
如果($ _SERVER ['REQUEST_METHOD'] =='POST'){
$name = $_POST["lanName"];
$lanName = $_POST["lanUsername"];
$password = $_POST["password"];
$phone = $_POST["phone"];
$address = $POST["address"];
$statement = mysqli_prepare($connect, "INSERT INTO landlord(lanName, lanUsername, password, phone, address) VALUES(?, ?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sssss", "$lanName", "$lanUsername", "$password", "$phone", "$address");
mysqli_stmt_execute($statement);
if ( mysqli_query($con, $sql) ) {
$result["success"] = "1";
$result["message"] = "success";
echo json_encode($result);
mysqli_close($con);
} else {
$result["success"] = "0";
$result["message"] = "error";
echo json_encode($result);
mysqli_close($con);
}
}
?>