如何获得数据进入数据库使用PHP和android工作室(服务器00webhost)



我已经使用000webhost创建了一个数据库,并将我的php文件上传到那里的服务器。使用安卓工作室,我试图创建一个用户注册页面。我点击进入注册页面,输入所有信息,点击提交按钮,它带我回到登录页面,因为它应该这样做,但是,信息没有发送到我的数据库。我没有收到错误信息,所以我不知道从哪里开始。下面是我的java类和php文件的代码,以及清单文件的一个片段。

注册请求java类

package amp.riot;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;

public class RegisterRequest extends StringRequest {
    private static final String REGISTER_REQUEST_URL ="https://upbeat.000webhostapp.com/register.php";
    private Map<String, String> params;
    public RegisterRequest(String name, String username, int age, String password,Response.Listener<String> listener){
        super (Method.POST, REGISTER_REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("name", name);
        params.put("username", username);
        params.put("password", password);
        params.put("age", age + "");
    }
    public Map<String, String> getParams(){
        return params;
    }
}

注册页java类

public class Register extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    final EditText etAge= (EditText) findViewById(R.id.etAge);
    final EditText etName= (EditText) findViewById(R.id.etName);
    final EditText etUsername= (EditText) findViewById(R.id.etUsername);
    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 name=etName.getText().toString();
            final String username=etUsername.getText().toString();
            final String password=etPassword.getText().toString();
            final int age= Integer.parseInt(etAge.getText().toString());
            final 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(Register.this, Login.class);
                            Register.this.startActivity(intent);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
                            builder.setMessage("Register Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            };
            RegisterRequest registerRequest = new RegisterRequest(name, username, age, password, responseListener);
            RequestQueue queue = Volley.newRequestQueue(Register.this);
            queue.add(registerRequest);
        }
    });
}

}

清单文件

<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="amp.riot">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

注册文件

<?php
$con = mysqli_connect("localhost", "id202020_sierra", "05alop59", "id202020_plank");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user(name, username, age, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;  
echo json_encode($response);

?>

尝试使用StringRequest代替Response.Listener<String>

StringRequest strreq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Toast.makeText(getApplicationContext(),response,Toast.LENGTH_SHORT).show();
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    }
}){
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> params = new HashMap<String, String>();
        params.put("id",id);
        params.put("name",editText_name.getText().toString());
        params.put("pass",editText_pass.getText().toString());
        return params;
    }
};
AppController.getInstance().addToRequestQueue(strreq);

您在php文件中犯了一个小错误。变化,

mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);

mysqli_stmt_bind_param($statement, "ssis", $name, $username, $age, $password);

这里,s表示String, i表示Integer。我相信用户名是字符串,年龄是int。谢谢你。

最新更新