I'm newbie and have been trying to make a login page for the first time. I've been following some videos on YouTube and been exactly Copy/Pasting the codes but I get this undefined index that says my username and password haven't been defined! I've made a test user in my database but can't login. the connection to the server is successful but the login page has user and pass issues.
My Login.php code:
<?php
require "connect.php";
$user_name =$_POST['user_name'];
$user_pass =$_POST['password'];
$mysql_qry = "select * from logintesttb where username like '$user_name' and password '$user_pass';";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0){
echo "Login was successful...!";
}
else{
echo "Oops! There was an error";
}
?>
My Java Code:
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://My IP Address/login.php";
if (type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8")+"&"+
URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
My OnClickListener for Login Button:
public void OnLogin(View view) {
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker (this);
backgroundWorker.execute (type, username, password);
}
please help me out if possible. thanks in advance...!