我是新手,并且第一次尝试创建登录页面。我一直关注YouTube上的一些视频并完全复制/粘贴代码,但我得到了这个未定义的索引,说我的用户名和密码尚未定义! 我在我的数据库中创建了一个测试用户,但无法登录。与服务器的连接成功,但登录页面有用户和通行证问题。
我的Login.php代码:
<?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";
}
?>
我的Java代码:
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;
}
我的登录按钮的OnClickListener:
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);
}
请尽可能帮助我。提前谢谢......!