I'm trying to make a register page for my android app. I have managed to create a login page and I'm now trying to get the register page to work.
I've managed to get the data I'm sending via the register page into the database. I'm using a php script to transfer the data into my mysql database.
But every time I register, it sends the data and gives the echo back saying that the username/mail already exists.
It also shows this error in Java: Invalid use of SingleClientConnManager: connection still allocated.
I've been trying to fix this for hours but can't find anything that helps.
This is my database:
id - primary key AI
mail - unique
password
username - unique
This is my php script
<?php
$hostname_memolink = "localhost";
$database_memolink = "memolink";
$username_memolink = "android";
$password_memolink ="f3.W2TmM}8yO";
$memolink = mysql_connect($hostname_memolink, $username_memolink, $password_memolink) or trigger_error(mysql_error(), E_USER_ERROR);
mysql_select_db($database_memolink, $memolink);
$mail = $_POST['mail'];
$password = $_POST['password'];
$username = $_POST['username'];
if (trim($password) == "" or trim($username) == "" or trim($mail) == "")
{
echo "variables are null";
exit;
}
else
{
$resultUsername = mysql_query("SELECT * FROM tbl_user WHERE username='$username'");
$resultMail = mysql_query("SELECT * FROM tbl_user WHERE mail='$mail'");
$num_rows_username = mysql_num_rows($resultUsername);
$num_rows_mail = mysql_num_rows($resultMail);
if ($num_rows_username >= 1 && $num_rows_mail >= 1) {
echo "username or email already exists";
die();
}
else
{
mysql_query("INSERT INTO tbl_user (id, mail, password, username) VALUES('', '$mail', '$password', '$username')") or die(mysql_error());
echo "registration complete";
}
}
?>
This is my Java method which sends the data to my php file:
HttpPost httpPost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpClient;
List<NameValuePair> registerData;
public void register(){
try{
httpClient = new DefaultHttpClient();
httpPost = new HttpPost("http://www.memo-link.be/php/register.php");
registerData = new ArrayList<NameValuePair>(2);
EditText mailBox = (EditText) findViewById(R.id.mail_box);
EditText userBox = (EditText) findViewById(R.id.username_box);
EditText passBox = (EditText) findViewById(R.id.password_box);
String mail = mailBox.getText().toString();
String username = userBox.getText().toString();
String password = passBox.getText().toString();
registerData.add(new BasicNameValuePair("mail", mail));
registerData.add(new BasicNameValuePair("username", username));
registerData.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(registerData));
response = httpClient.execute(httpPost); //send data to Internet
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpClient.execute(httpPost, responseHandler);
System.out.println("Response : " + response.toString());
if (response.equalsIgnoreCase("username or email already exists")){
runOnUiThread(new Runnable(){
public void run(){
dialog.dismiss();
}
});
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(250);
Toast toast = Toast.makeText(RegisterActivity.this, "Username already exists!", Toast.LENGTH_SHORT);
toast.show();
}else if(response.toString().equalsIgnoreCase("username or email already exists")){
runOnUiThread(new Runnable(){
public void run(){
dialog.dismiss();
}
});
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(250);
Toast toast = Toast.makeText(RegisterActivity.this, "Mail address already in use!", Toast.LENGTH_SHORT);
toast.show();
}else if(response.toString().equalsIgnoreCase("registration complete")){
runOnUiThread(new Runnable(){
public void run(){
Toast toast = Toast.makeText(RegisterActivity.this, "Registration Succesfull", Toast.LENGTH_SHORT);
toast.show();
}
});
startActivity(new Intent(RegisterActivity.this, DashboardActivity.class));
}
runOnUiThread(new Runnable(){
public void run(){
dialog.dismiss();
}
});
}catch (Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
Thanks in advance