PHP returns undefined index to Android studio [dup

2019-08-20 03:26发布

Im trying to connect Android Studio to a localhost MySQL server. I believe something is wrong with my php file, and I don't know how to fix it. I tried using isset, but that doesn't seem to work. Its supposed to be a simple registration script

<?php
    $db_host = '192.168.4.174:3306';
 $db_user = 'root';
 $db_pass = '';
 $db_name = 'test';

 $con = mysqli_connect($db_host,'root',$db_pass,$db_name);
 if($con){
  echo "connection successful";
 }
 if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
 }

    $isAdmin = $_POST["isAdmin"];//line 16
    $username = $_POST["username"];
    $password = $_POST["password"];
 if ($isAdmin = null){
  $isAdmin = 0;
 }
 if ($username = null){
  $username = "fakename";
 }
 if ($password = null){
  $password = "fakepassword";
 }
    $statement = mysqli_prepare($con, "INSERT INTO cresidentials (username,password,isAdmin) VALUES (?, ?, ?)");
    mysqli_stmt_bind_param($statement,'ssi',$username,$password,$isAdmin);
    mysqli_stmt_execute($statement);
    if(!$statement) { printf("Prepare failed: %s\n", mysqli_error($con)); }
    echo "success";
?>

The error says

undefined index: isAdmin, username, password at line 16-18

I tried replacing POST with isset, but that doesn't seem to work. It may be how its getting the info, so ill provide the registerrequest here;

public class RegisterRequest extends StringRequest {

    private static final String REGISTER_REQUEST_URL = "http://192.168.*.*:80/phptesting/Register.php";
    private Map<String, String> params;
    public RegisterRequest(String username, String password,String isAdmin, Response.Listener<String> listener){
        super(Method.POST, REGISTER_REQUEST_URL,listener,null);
        params = new HashMap<>();
        params.put("username",username);
        params.put("password",password);
        params.put("isAdmin",isAdmin+"");
    }

    public Map<String, String> getparams() {
        return params;
    }
}

I tried to echo my variables, since everyone seems to do that, but i still get nothing. Isset just doesn't seem to do anything, and gives the same error. I might be using it wrong, however.

And just in case I'm not using jsonobjects properly, here is the register script;

public class CreateUser extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_user);
        this.setTitle("Create User");
        final EditText username1 = findViewById(R.id.Createusername);
        final EditText password1 = findViewById(R.id.CreatePassword);
        final Switch isAdmin = findViewById(R.id.isadmin);
        final Button createuser = findViewById(R.id.createuserbtn);
        if (getIntent().hasExtra("com.example.northlandcaps.crisis_response")){
            isAdmin.setVisibility(View.GONE);
        }
        createuser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String username = username1.getText().toString();
                final String password = password1.getText().toString();
                final String isadmin = isAdmin.getText().toString();
                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("Response Value: ", response);
                            if (response.equals("success")){
                                Intent intent = new Intent(CreateUser.this, MainActivity.class);
                                CreateUser.this.startActivity(intent);
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(CreateUser.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry",null)
                                        .create()
                                        .show();
                        }
                    }
                };
                RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener);
                RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
                queue.add(registerRequest);
            }
        });
    }

Thanks for the help

0条回答
登录 后发表回答