我制作了一个基本的Android应用程序,注册活动和登录活动,我使用免费的网络托管(000webhost)数据库。
一切正常,但是当我使用注册活动表单(电子邮件和密码)注册时,它不会将用户凭据保存到数据库中。它甚至没有显示错误。我已经检查了代码,到目前为止没有错误。
<?php
$con = mysqli_connect("host", "database user", "password", "database
name");
$email = $_POST["email"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO database name (email,
password) VALUES (?, ?)");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etEmail = (EditText) findViewById(R.id.etEmail);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bRegister = (Button) findViewById(R.id.bRegister);
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = etEmail.getText().toString();
final String password = etPassword.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(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(email, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "";
private Map<String, String> params;
public RegisterRequest(String email, String password, Response.Listener<String> listener){
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("email", email);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
答案 0 :(得分:0)
注意区别:你有一个数据库服务器。在该服务器上是一个数据库(或多个)。这样的数据库可以用于某些网站/项目。在该数据库中有几个表。
在此表中存储信息。
您的插入查询要插入“数据库名称” 你应该插入某个表
答案 1 :(得分:0)
除非您的表名是&#34;数据库名称&#34;,否则这就是它无法运作的原因。另外,我不认为mySQL允许表名中有空格,但我可能错了。
$statement = mysqli_prepare($con, "INSERT INTO database name (email,
password) VALUES (?, ?)");
应替换为
$statement = mysqli_prepare($con, "INSERT INTO table_name (email,
password) VALUES (?, ?)");