-3

I want to build app that users can regist a username and password and the data send to MySQL database using PHP this is my PHP code

$connect = mysqli_connect("mysql1.000webhost.com","a5900443_jekn","123456","a5900443_jek");

if(mysqli_connect_errno($connect))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
    echo "success";
}

$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';

$query = mysqli_query($connect, "insert into users (username, password) values ('$username' ,'$password') ");

mysqli_close($connect);
?>

I try different Android project but the data doesn't save in database can you give me how to send the username and password from the Android app to this PHP code?

This is my Android Java:

public class MainActivity extends Activity{

StrictMode.ThreadPolicy policy = new           StrictMode.ThreadPolicy.Builder().permitAll().build();

private EditText username;

private EditText password;



public void onCreate(Bundle savedInstanceState)

{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     username = (EditText) findViewById(R.id.e1);
    password = (EditText) findViewById(R.id.e2);

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {

                String result = null;
                InputStream is = null;
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("username",username.getText().toString
                                                          ()));

                nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString
                                                          ()));

                StrictMode.setThreadPolicy(policy); 


                //http post
                try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://fkrucusowtcl.web44.net/register.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost); 
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    Log.e("log_tag", "connection success ");
                    Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
                }


                catch(Exception e)
                {
                    Log.e("log_tag", "Error in http connection "+e.toString());
                    Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

                }
                //convert response to string
                try{
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) 
                    {

                    }
                    is.close();

                    result=sb.toString();
                }
                catch(Exception e)
                {
                    Log.e("log_tag", "Error converting result "+e.toString());
                }


                try{

                    JSONObject json_data = new JSONObject(result);

                    CharSequence w= (CharSequence) json_data.get("re");

                    Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();


                }
                catch(JSONException e)
                {
                    Log.e("log_tag", "Error parsing data "+e.toString());
                    Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                }



            }
        });



}

Where is the wrong it doesn't send data to PHP

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • possible duplicate of [connecting PHP webservices with Android](http://stackoverflow.com/questions/2205885/connecting-php-webservices-with-android) – NovusMobile Jul 21 '15 at 04:37

1 Answers1

0

Take a look at this http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/

This will guide you on how to create a REST api, This will allow you to register and do other functions with your database securely.

Errol Green
  • 1,367
  • 5
  • 19
  • 32
  • I want another android project – user5056294 Jul 21 '15 at 07:49
  • @user5056294 You should really consider looking at the tutorial, everything you asked is explained inside. You have to understand that you cant just store passwords in plain-text, they need to be hashed in order to be secured(which is also described in the tutorial) . Also if you are registering a user their are certain security protocols that you should follow such as oauth. I'd highly recommend reading into this tutorial if you want to properly register/authenticate your users. – Errol Green Jul 21 '15 at 08:03
  • Also as Almas said above in his answer, a framework will provide much better security then your current code as it is majorly exposed. Slim framework is also integrated into tutorial. – Errol Green Jul 21 '15 at 08:06
  • I wont to secured the passworf I just want to learn how to send data to php – user5056294 Jul 21 '15 at 08:28