I have this RegisterActivity.java
where the user registers username and password, when the user presses ok
I am always getting the dialog containing user already exists
while the user doesn't exist and it is added successfully to the database.
here is the code of RegisterActivity.java
package info.androidhive.slidingmenu;
public class RegisterActivity extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
b = (Button)findViewById(R.id.button1);
et = (EditText)findViewById(R.id.username);
pass= (EditText)findViewById(R.id.password);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(RegisterActivity.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
Register();
}
}).start();
}
});
}
void Register(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://192.168.1.112/RegisterConnectNow.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
// edited by James from coderzheaven.. from here....
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
if(response.equalsIgnoreCase("1")){
dialog.dismiss();
showAlertSuccess();
}else if(response.equalsIgnoreCase("2")){
dialog.dismiss();
showAlertError();
}
else{
dialog.dismiss();
displayToast("error");
}
}catch(Exception e){
dialog.dismiss();
showAlertInternet();
}
}
public void showAlertSuccess(){
RegisterActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(RegisterActivity.this)
.setTitle("Success")
.setMessage("User Added Successfully")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
public void showAlertError(){
RegisterActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(RegisterActivity.this)
.setTitle("Error")
.setMessage("User Already exists")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
public void showAlertInternet(){
RegisterActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(RegisterActivity.this)
.setTitle("Error")
.setMessage("No Connection to the server")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.register, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void displayToast(String s){
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}
and here is the php code of RegisterConnectNow.php
<?php
require_once 'DatabaseConnectNow.php';
$username = $_POST['username'];
$password = $_POST['password'];
$query_search = "select * from userstable where username = '".$username."'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query
_exec);
if($rows == 0) {
$query = "INSERT INTO userstable (username,password)
VALUES ('$username', '$password')";
mysql_query($query);
echo "1";
}
else {
echo "2";
}
?>
So My code is behaving Correctly with respect to the database , users are added when they are not duplicate and not added when they already exist. The problem is that the response is always returning as 2
so it is displaying user already exists