用一个php文件将数据插入两个具有关系(PK,FK“ user_id”)的表中

时间:2018-10-17 15:19:31

标签: php android sql android-volley

我有两个表

T1“用户”:user_id(PK&Autoincrement)|名称|电邮|密码成绩

T2“得分”:user_id(FK&AI)|数学|信息

到目前为止,我的php文件如下所示:

<?php
    $connect = mysqli_connect("localhost", "id6777294_l2w", "123l2w456", "id6777294_l2w");
    
if(isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["password"]) && isset($_POST["grade"])){
    $name = $_POST["name"];
    $email = $_POST["email"];
    $password = $_POST["password"];
    $grade = $_POST["grade"];
}

    $statement = mysqli_prepare($connect, "INSERT INTO user(name, email, password, grade) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "sssi", $name, $email, $password, $grade);
    mysqli_stmt_execute($statement);
    
    $response = array();
    $response["success"] = true;  
    
    echo json_encode($response);
?>

这是我的客户端(Android)最初的StringRequest

public class RegisterRequest extends StringRequest {

    private static final String REGISTER_REQUEST_URL = "http://*****************/Register.php";

   
    private Map<String, String> params;

  
    public RegisterRequest(String name, String email, String password, int grade,
                           Response.Listener<String> listener){

    
        super(Request.Method.POST, REGISTER_REQUEST_URL, listener, null);

        params = new HashMap<>();
        params.put("name", name);
        params.put("email", email);
        params.put("password", password);
        params.put("grade", grade + "");

    }

  
    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

和活动

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        final EditText nameID = (EditText) findViewById(R.id.nameID);
        final EditText emailID = (EditText) findViewById(R.id.emailID);
        final EditText passwordID = (EditText) findViewById(R.id.passwordID);
        final EditText gradeID = (EditText) findViewById(R.id.gradeID);
        final Button registerbtn = (Button) findViewById(R.id.registerbtn);


        registerbtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                final String name = nameID.getText().toString();
                final String email = emailID.getText().toString();
                final String password = passwordID.getText().toString();
                final int grade = Integer.parseInt(gradeID.getText().toString());

                //now we need a String Listener for the constructor
                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){
                                startLoginActivity();
                            }else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("Registrierung fehlgeschlagen").setNegativeButton("Wiederhole",null).create().show();

                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                };

                RegisterRequest registerRequest = new RegisterRequest(name, email, password, grade, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
                queue.add(registerRequest);
            }
        });


    }

    private void startLoginActivity(){
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        RegisterActivity.this.startActivity(intent);
    }
}

此代码可以正常工作,并通过注册插入新用户。但是现在我要通过php文件填写两个表,分别是一个用户和他的得分(与PK / FK user_id有关)。

任何人都可以帮助我,我被困在这里。我不知道如何更改php文件和Java代码。

0 个答案:

没有答案