I have a simple registration script, but android studio isnt properly putting data into the php script. I ran the php script on a browser, and it puts data in just fine, but android studio isnt doing it right.
Here is the registerrequest script.
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,
Response.ErrorListener errListener){
super(Method.POST, REGISTER_REQUEST_URL,listener,errListener);
params = new HashMap<>();
params.put("username",username);
params.put("password",password);
params.put("isAdmin",isAdmin+"");
}
public Map<String, String> getparams() {
return params;
}
}
here is the create user 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();
}
}
};Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), String.valueOf(error), Toast.LENGTH_SHORT).show();
}
};
RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener,errorListener);
RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
queue.add(registerRequest);
}
});
}
here is the php script
<?php
$db_host = '192.168.*.*:3306';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
var_dump($_POST["username"]);//line 6
$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();
}
if(isset($_POST["isAdmin"]) && isset($_POST["username"]) && isset($_POST["password"]))
{
$username = $_POST["username"];
$password = $_POST["password"];
$isAdmin = $_POST["isAdmin"];
$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";
}
else
echo "values not set";
?>
When i run this, the app creates the retry error popup message. And the output is this;
Notice: Undefined index: username in C:\xampp\htdocs\phptesting\Register.php on line 6 NULL connection successfulvalues not set
I believe its complaining that username isnt anything. Which is a huge problem because other variables are defined below, and I do not know if they got through if the script stopped this early I appreciate all help, thank you