Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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);
    
 // Check connection
 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:
Java
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";

        // Request a string response from the provided URL.
        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)
            {
                // Something went wrong
                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;
            }
        };

        // Add the request to the RequestQueue.
        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
Posted
Updated 16-Sep-22 19:46pm

1 solution

Don't do it like that! Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
 
Share this answer
 
Comments
Doomsday Apr2022 17-Sep-22 2:42am    
This not commercial project.
Just for understanding.
Its not working why
OriginalGriff 17-Sep-22 3:00am    
Doesn't matter: if you get into bad habits now, they tend to stick and make your life a lot harder later.
Get into the habit of doing "the right thing" and it becomes second nature: hashed, salted passwords, parameterized queries, etc.. It's worth doing!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900