-2

I've make a project user login in android, How can I check with a simple code like a PHP (lol) if a user already exists then make a toast. Have you any sugestions?

here is my java

protected String doInBackground(String... args) {

    List<NameValuePair> params = new ArrayList<NameValuePair>();         params.add(new BasicNameValuePair("nama", namauser.getText().toString()));
    params.add(new BasicNameValuePair("fakunit", spPropellant.getSelectedItem().toString()));
    params.add(new BasicNameValuePair("notelp", notelpon.getText().toString()));
    params.add(new BasicNameValuePair("email", email.getText().toString()));
    //params.add(new BasicNameValuePair("npwp", username.getText().toString()));
    params.add(new BasicNameValuePair("password", password.getText().toString()));

            JSONObject json = jsonParser.makeHttpRequest(url_insert,
            "POST", params);

    // check log cat fro response
    Log.d("Create Response", json.toString());

    // check for success tag
    try {
        int success = json.getInt(TAG_SUCCESS); //ambil success value yang dikirim php

        if (success == 1) { // cek jika variable success = 1, berarti registrasi berhasil
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(getApplicationContext(), "Register Success. Please Log in!", Toast.LENGTH_LONG).show();
                }});

            Intent i = new Intent(Register.this, Login.class);
            startActivity(i);
            finish();
        }
        else {
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(getApplicationContext(), "Register GAGAL.", Toast.LENGTH_LONG).show();
                }});

            Intent i = new Intent(Register.this, Register.class);
            startActivity(i);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}


protected void onPostExecute(String file_url) {
    // dismiss the dialog once done
    dialog.dismiss();
}

and this php code register phpregister

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

The email field should be unique in the database. You need to check if the email already exists before inserting data.

I believe this answers your question: How to check if user already exists in MySQL with PHP

radu.bn
  • 21
  • 3
  • When inserting data you can receive more than one type of error, not necessarily that the email is unique. I still believe that checking for the email beforehand is better. If you receive an error afterwards, you know it's not because of the email. – radu.bn Oct 18 '17 at 08:19