0

What you see below in PHP and JAVA files are a complete (although simple) registration functionality. It all works.

The question now is,

How do I detect if the username field is already taken , and how do I let the java side know that so I can give the user a proper message.

ORIGINAL register.php:

<?php

define('HOST','X');
define('USER','X');
define('PASS','X');
define('DB','X');

$con = mysqli_connect(HOST,USER,PASS,DB);

 $name = $_POST['name'];
 $pass = $_POST['pass'];

  $sql = "insert into tbl_user (username,password) values ('$name','$pass')";
  if(mysqli_query($con,$sql)){
    echo 'success';
  }
  else{
    echo 'failure';
  }
  mysqli_close($con);
?>

Part of register.java:

private void insertToDatabase(String name, String pass) {
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            String name = params[0];
            String pass = params[1];

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("name", name));
        nameValuePairs.add(new BasicNameValuePair("pass", pass));

        boolean exists = false;


        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(
                    "http://calisapp.esy.es/register.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpClient.execute(httpPost);

            HttpEntity entity = response.getEntity();


        } catch (ClientProtocolException e) {

        } catch (IOException e) {

        }
        return "";
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        Intent intent = new Intent(Register.this, MainActivity.class);

        startActivity(intent);
        Toast.makeText(Register.this, "Registered successfully", Toast.LENGTH_LONG).show();
        finish();
    }
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, pass);

}

Calis
  • 47
  • 1
  • 11
  • `if (response == 'taken') { show 'not available' } else { show 'available'}`, basically – Marc B Jul 23 '15 at 18:51
  • You're mixing `mysql` and `mysqli` APIs here, which is probably the problem you are experiencing. There's also a SQL injection vulnerability, which needs to be fixed with parameter binding. – halfer Jul 23 '15 at 18:52
  • Since you're doing an insert immediately without a preceding select, am I right in assuming you have a unique constraint on the `username` column? – halfer Jul 23 '15 at 18:53
  • Yes I have a unique constraint. I just want the java side to know that so the user doesnt think the registration worked. Also I'm not experiencing a problem, I'm missing the functionality I want to add. – Calis Jul 23 '15 at 19:34
  • Let's take it back a step to make it more logical. Ill add the original php code to the question. This will make the php code only do a simple registration. The question then is, how do I detect if the username field is already taken, and how do I let the java side know that so I can give the user a proper message. – Calis Jul 23 '15 at 19:36
  • I edited the main post. It should now be clear what I have and what I'm missing. – Calis Jul 23 '15 at 19:44

0 Answers0