Android studio isnt posting data in the PHP script “Unidentified index”
up vote
0
down vote
favorite
I have a simple register request script. My Android Studio is supposed to send 3 string over to the URL(php script), but the PHP doesn't receive any data, or is handling it poorly.
This is the RegisterRequest script:(its supposed to recieve values from within android studio and then push the info into the PHP script using POST method, located on localhost/ip address)
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;
}
}
Here is the CreateUser script. Its supposed to grab strings from the XML and then call the RegisterRequest class and hand over the strings;
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);
}
});
And finally, here is my PHP script, which should receive Strings and put them into the db;
<?php
$db_host = 'localhost:3306';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$con = mysqli_connect($db_host,'user',$db_pass,$db_name);
if($con){
echo "connection successful";
}else{
echo "connection failed";
}
$age = $_POST["isAdmin"];//line 14
$username = $_POST["username"];//line 15
$password = $_POST["password"];//line 16
$statement = mysqli_prepare($con, "INSERT INTO cresidentials (username,password,isAdmin) VALUES (?, ?, ?)");
if(!$statement) { printf("Prepare failed: %sn", mysqli_error($con)); }
if(!$statement) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }
mysqli_stmt_bind_param($statement, "ssi",$username,$password,$isAdmin);
mysqli_stmt_execute($statement);
if(mysqli_error($con)) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }//line 22
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
I hope my explanation of the script functions help you guys on debugging the problem. Also, android studio outputs this;
Notice: Undefined index: isAdmin in Register.php on line 14
Notice: Undefined index: username in Register.php on line 15
Notice: Undefined index: password in Register.php on line 16
(Is it Android studio not sending the data over? or PHP not assigning what is receiving? does PHP need a method just to put data in?)
Thank you for your help
java php
|
show 2 more comments
up vote
0
down vote
favorite
I have a simple register request script. My Android Studio is supposed to send 3 string over to the URL(php script), but the PHP doesn't receive any data, or is handling it poorly.
This is the RegisterRequest script:(its supposed to recieve values from within android studio and then push the info into the PHP script using POST method, located on localhost/ip address)
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;
}
}
Here is the CreateUser script. Its supposed to grab strings from the XML and then call the RegisterRequest class and hand over the strings;
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);
}
});
And finally, here is my PHP script, which should receive Strings and put them into the db;
<?php
$db_host = 'localhost:3306';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$con = mysqli_connect($db_host,'user',$db_pass,$db_name);
if($con){
echo "connection successful";
}else{
echo "connection failed";
}
$age = $_POST["isAdmin"];//line 14
$username = $_POST["username"];//line 15
$password = $_POST["password"];//line 16
$statement = mysqli_prepare($con, "INSERT INTO cresidentials (username,password,isAdmin) VALUES (?, ?, ?)");
if(!$statement) { printf("Prepare failed: %sn", mysqli_error($con)); }
if(!$statement) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }
mysqli_stmt_bind_param($statement, "ssi",$username,$password,$isAdmin);
mysqli_stmt_execute($statement);
if(mysqli_error($con)) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }//line 22
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
I hope my explanation of the script functions help you guys on debugging the problem. Also, android studio outputs this;
Notice: Undefined index: isAdmin in Register.php on line 14
Notice: Undefined index: username in Register.php on line 15
Notice: Undefined index: password in Register.php on line 16
(Is it Android studio not sending the data over? or PHP not assigning what is receiving? does PHP need a method just to put data in?)
Thank you for your help
java php
I believe$_POSTneeds to be used when you are receiving data using HTTP POST method. Where as you are using PUT method from android. That could be an issue.
– Nik
Nov 8 at 5:02
@Nik So how does PHP need to be structured to receive this "raw" PUT from Android?
– Alec Harvey
Nov 8 at 5:03
Check this PHP Manual page: php.net/manual/en/features.file-upload.put-method.php. Just curious why are you using PUT method and not POST?
– Nik
Nov 8 at 5:15
@Nik i am actually using PUT because my script is a culmination of StackOverflow, Indian YT videos, and unhelpful manual/guide pages for syntax. But if you suggest to replace PUT with POST, then ill try it
– Alec Harvey
Nov 8 at 5:16
1
@Nik i cannot seem to replace put in android with POST, so it seems like that is an Android thing
– Alec Harvey
Nov 8 at 5:18
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a simple register request script. My Android Studio is supposed to send 3 string over to the URL(php script), but the PHP doesn't receive any data, or is handling it poorly.
This is the RegisterRequest script:(its supposed to recieve values from within android studio and then push the info into the PHP script using POST method, located on localhost/ip address)
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;
}
}
Here is the CreateUser script. Its supposed to grab strings from the XML and then call the RegisterRequest class and hand over the strings;
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);
}
});
And finally, here is my PHP script, which should receive Strings and put them into the db;
<?php
$db_host = 'localhost:3306';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$con = mysqli_connect($db_host,'user',$db_pass,$db_name);
if($con){
echo "connection successful";
}else{
echo "connection failed";
}
$age = $_POST["isAdmin"];//line 14
$username = $_POST["username"];//line 15
$password = $_POST["password"];//line 16
$statement = mysqli_prepare($con, "INSERT INTO cresidentials (username,password,isAdmin) VALUES (?, ?, ?)");
if(!$statement) { printf("Prepare failed: %sn", mysqli_error($con)); }
if(!$statement) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }
mysqli_stmt_bind_param($statement, "ssi",$username,$password,$isAdmin);
mysqli_stmt_execute($statement);
if(mysqli_error($con)) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }//line 22
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
I hope my explanation of the script functions help you guys on debugging the problem. Also, android studio outputs this;
Notice: Undefined index: isAdmin in Register.php on line 14
Notice: Undefined index: username in Register.php on line 15
Notice: Undefined index: password in Register.php on line 16
(Is it Android studio not sending the data over? or PHP not assigning what is receiving? does PHP need a method just to put data in?)
Thank you for your help
java php
I have a simple register request script. My Android Studio is supposed to send 3 string over to the URL(php script), but the PHP doesn't receive any data, or is handling it poorly.
This is the RegisterRequest script:(its supposed to recieve values from within android studio and then push the info into the PHP script using POST method, located on localhost/ip address)
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;
}
}
Here is the CreateUser script. Its supposed to grab strings from the XML and then call the RegisterRequest class and hand over the strings;
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);
}
});
And finally, here is my PHP script, which should receive Strings and put them into the db;
<?php
$db_host = 'localhost:3306';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$con = mysqli_connect($db_host,'user',$db_pass,$db_name);
if($con){
echo "connection successful";
}else{
echo "connection failed";
}
$age = $_POST["isAdmin"];//line 14
$username = $_POST["username"];//line 15
$password = $_POST["password"];//line 16
$statement = mysqli_prepare($con, "INSERT INTO cresidentials (username,password,isAdmin) VALUES (?, ?, ?)");
if(!$statement) { printf("Prepare failed: %sn", mysqli_error($con)); }
if(!$statement) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }
mysqli_stmt_bind_param($statement, "ssi",$username,$password,$isAdmin);
mysqli_stmt_execute($statement);
if(mysqli_error($con)) { return json_encode(['status'=>'failed','message'=>mysqli_error($con)]); }//line 22
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
I hope my explanation of the script functions help you guys on debugging the problem. Also, android studio outputs this;
Notice: Undefined index: isAdmin in Register.php on line 14
Notice: Undefined index: username in Register.php on line 15
Notice: Undefined index: password in Register.php on line 16
(Is it Android studio not sending the data over? or PHP not assigning what is receiving? does PHP need a method just to put data in?)
Thank you for your help
java php
java php
edited Nov 8 at 5:45
Nik
1169
1169
asked Nov 8 at 4:57
Alec Harvey
388
388
I believe$_POSTneeds to be used when you are receiving data using HTTP POST method. Where as you are using PUT method from android. That could be an issue.
– Nik
Nov 8 at 5:02
@Nik So how does PHP need to be structured to receive this "raw" PUT from Android?
– Alec Harvey
Nov 8 at 5:03
Check this PHP Manual page: php.net/manual/en/features.file-upload.put-method.php. Just curious why are you using PUT method and not POST?
– Nik
Nov 8 at 5:15
@Nik i am actually using PUT because my script is a culmination of StackOverflow, Indian YT videos, and unhelpful manual/guide pages for syntax. But if you suggest to replace PUT with POST, then ill try it
– Alec Harvey
Nov 8 at 5:16
1
@Nik i cannot seem to replace put in android with POST, so it seems like that is an Android thing
– Alec Harvey
Nov 8 at 5:18
|
show 2 more comments
I believe$_POSTneeds to be used when you are receiving data using HTTP POST method. Where as you are using PUT method from android. That could be an issue.
– Nik
Nov 8 at 5:02
@Nik So how does PHP need to be structured to receive this "raw" PUT from Android?
– Alec Harvey
Nov 8 at 5:03
Check this PHP Manual page: php.net/manual/en/features.file-upload.put-method.php. Just curious why are you using PUT method and not POST?
– Nik
Nov 8 at 5:15
@Nik i am actually using PUT because my script is a culmination of StackOverflow, Indian YT videos, and unhelpful manual/guide pages for syntax. But if you suggest to replace PUT with POST, then ill try it
– Alec Harvey
Nov 8 at 5:16
1
@Nik i cannot seem to replace put in android with POST, so it seems like that is an Android thing
– Alec Harvey
Nov 8 at 5:18
I believe
$_POST needs to be used when you are receiving data using HTTP POST method. Where as you are using PUT method from android. That could be an issue.– Nik
Nov 8 at 5:02
I believe
$_POST needs to be used when you are receiving data using HTTP POST method. Where as you are using PUT method from android. That could be an issue.– Nik
Nov 8 at 5:02
@Nik So how does PHP need to be structured to receive this "raw" PUT from Android?
– Alec Harvey
Nov 8 at 5:03
@Nik So how does PHP need to be structured to receive this "raw" PUT from Android?
– Alec Harvey
Nov 8 at 5:03
Check this PHP Manual page: php.net/manual/en/features.file-upload.put-method.php. Just curious why are you using PUT method and not POST?
– Nik
Nov 8 at 5:15
Check this PHP Manual page: php.net/manual/en/features.file-upload.put-method.php. Just curious why are you using PUT method and not POST?
– Nik
Nov 8 at 5:15
@Nik i am actually using PUT because my script is a culmination of StackOverflow, Indian YT videos, and unhelpful manual/guide pages for syntax. But if you suggest to replace PUT with POST, then ill try it
– Alec Harvey
Nov 8 at 5:16
@Nik i am actually using PUT because my script is a culmination of StackOverflow, Indian YT videos, and unhelpful manual/guide pages for syntax. But if you suggest to replace PUT with POST, then ill try it
– Alec Harvey
Nov 8 at 5:16
1
1
@Nik i cannot seem to replace put in android with POST, so it seems like that is an Android thing
– Alec Harvey
Nov 8 at 5:18
@Nik i cannot seem to replace put in android with POST, so it seems like that is an Android thing
– Alec Harvey
Nov 8 at 5:18
|
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201786%2fandroid-studio-isnt-posting-data-in-the-php-script-unidentified-index%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I believe
$_POSTneeds to be used when you are receiving data using HTTP POST method. Where as you are using PUT method from android. That could be an issue.– Nik
Nov 8 at 5:02
@Nik So how does PHP need to be structured to receive this "raw" PUT from Android?
– Alec Harvey
Nov 8 at 5:03
Check this PHP Manual page: php.net/manual/en/features.file-upload.put-method.php. Just curious why are you using PUT method and not POST?
– Nik
Nov 8 at 5:15
@Nik i am actually using PUT because my script is a culmination of StackOverflow, Indian YT videos, and unhelpful manual/guide pages for syntax. But if you suggest to replace PUT with POST, then ill try it
– Alec Harvey
Nov 8 at 5:16
1
@Nik i cannot seem to replace put in android with POST, so it seems like that is an Android thing
– Alec Harvey
Nov 8 at 5:18