I am working on a simple android app which inserts Name & Password from user to MySQL DB. I am using free web hosting platform(000webhost). When I hardcode input from users to SQL query in my PHP script on web server and run it in the browser it works fine.
Problem : When I run my app it doesn't work and shows Undefined Index because of Empty input from user even though the input is not empty. So I think i am unable to pass user input from android to PHP script. How should i do that correctly ? What i am missing ?
This is my PHP Script :
<?php
$serverName="localhost";
$databaseName="xxxxxxxxx";
$userName="xxxxxxxx";
$password="xxxxxxxx";
$name=$_POST['username'];
$pass=$_POST['userpassword'];
if(isset($_POST['username'])){
$name = $_POST['username'];
}else{
$name = "Name Empty";
}
if(isset($_POST['userpassword'])){
$pass = $_POST['userpassword'];
}else{
$pass = "Password Empty";
}
$con=mysqli_connect($serverName,$userName,$password,$databaseName);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "INSERT into xxxxxxx (name, password) VALUES ('$name','$pass')";
if ($con->query($sql) === TRUE) {
echo "Sucessful";
echo "Name :".$name;
echo "Password :".$pass;
} else {
echo "Error: ";
}
$con->close();
?>
This is my Android part:
public void insertdata(View v)
{
Name = name.getText().toString();
Password = pass.getText().toString();
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url ="https://xxxx.com/xxxxx/xxxx.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET, 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)
{
Toast.makeText(getApplicationContext(), "Some error :"+"\n"+ error.toString(),Toast.LENGTH_SHORT).show();
}
})
{
@Nullable
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
Map<String, String> datatoinsert = new HashMap<String, String>();
datatoinsert.put("username",Name);
datatoinsert.put("userpassword",Password);
return datatoinsert;
}
};
queue.add(stringRequest);
}
What I have tried:
I am using Hashmap for mapping.
I dont know where i am going wrong and what else to try.
Help me