I am trying to sent data from my android app to php server for login .... but It respone me $success=1 and #message=login successfully .... even if I enter wrong username and password ..... can anyone help me?
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
echo $username;
echo $password;
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql checking row
$result = mysql_query("SELECT * FROM user WHERE username = '$username' AND password = '$password'")or die(mysql_error());
// check if row exist or not
if ($result == true) {
// row exist in database
$response["success"] = 1;
$response["message"] = "Login successfully.";
// echoing JSON response
echo json_encode($response);
} else {
// row not exist
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
on android side:
class NewSignup extends AsyncTask<String, String, String> {
/**
* Creating product
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username_editTextString));
params.add(new BasicNameValuePair("password", password_editTextString));
// getting JSON Object
// Note that create product url accepts POST method
json = jsonParser.makeHttpRequest(url_create_login,
"POST", params);
// check log cat fro response
Log.i("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully login
Intent i = new Intent(getApplicationContext(), Next.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to login
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}