[英]how to verify login input with database php MySQL
please help i'm doing a loging activity for my android application, i checked the email and password with my database using this code请帮助我为我的 android 应用程序进行登录活动,我使用此代码检查了我的数据库中的电子邮件和密码
<?php
try
{
// On se connecte à MySQL
$bdd = new PDO('mysql:host=localhost;dbname=application;charset=utf8', 'root', '');
}
catch(Exception $e)
{
// En cas d'erreur, on affiche un message et on arrête tout
die('Erreur : '.$e->getMessage());
echo 'base de donnee n est pas connecte';
}
//Getting values
$username = $_POST['editusername'];
$password = $_POST['editpassword'];
//contact surgat
// On affiche chaque entrée une à une
$reponse = $bdd->query("SELECT * FROM personne WHERE Email='$username' AND Motpass='$password'");
while ($donnees = $reponse->fetch())
{
//if we got some result
if(isset($donnees)){
//displaying success
echo "success";
}else{
//displaying failure
echo "failure";
}
$reponse->closeCursor();
}
?>
and i'm working with android studio this is the loginactivity.java that i used in it AsyncTask to get the result我正在与 android studio 合作,这是我在 AsyncTask 中使用的 loginactivity.java 来获取结果
package com.example.yh.log;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MainActivity extends Activity {
private EditText TextUserName;
private EditText TextPassword;
public static final String USER_NAME = "USERNAME";
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onLoginClick(View view){
TextUserName = (EditText) findViewById(R.id.editusername);
TextPassword = (EditText) findViewById(R.id.editpassword);
username=TextUserName.getText().toString();
password=TextPassword.getText().toString();
if(username.isEmpty())
Toast.makeText(getBaseContext(),"Entrez votre username",Toast.LENGTH_SHORT).show();
else if(password.isEmpty())
Toast.makeText(getBaseContext(),"Entrez votre mot de passe",Toast.LENGTH_SHORT).show();
else {
String urlString = "http://192.168.173.1/Search/login.php";
LoginTask loginTask = new LoginTask();
loginTask.execute(urlString);
}
}
private class LoginTask extends AsyncTask<String,Void,String>
{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
HttpURLConnection c=null;
try {
String urlString=params[0];
URL url=new URL(urlString);
c=(HttpURLConnection)url.openConnection();
c.setRequestMethod("POST");
c.setConnectTimeout(15000 /* milliseconds */);
c.setDoInput(true);
c.setDoOutput(true);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String s = "username="+username+"&password=" + password;
c.setFixedLengthStreamingMode(s.getBytes().length);
PrintWriter out = new PrintWriter(c.getOutputStream());
out.print(s);
out.close();
c.connect();
int mStatusCode = c.getResponseCode();
String result="";
switch (mStatusCode) {
case 200:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
result = sb.toString();
}
return result;
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
return "Error connecting to server";
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
@Override
protected void onPostExecute(String s) {
//super.onPostExecute(s);
String ss = s;
loadingDialog.dismiss();
if(ss=="success") {
Intent intent = new Intent(MainActivity.this, UserProfile.class);
intent.putExtra(USER_NAME, username);
finish();
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "invalide username or password", Toast.LENGTH_LONG).show();
}}
}
}
i keep geting this error in php code "indefined index editusername" when i run the activity and hit the button althought i declared the edit text with same ID in loginactivity.xml i have changed it many times but still not working当我运行活动并点击按钮时,我一直在 php 代码“indefined index editusername”中收到此错误,尽管我在 loginactivity.xml 中声明了具有相同 ID 的编辑文本,但我已对其进行了多次更改,但仍然无法正常工作
<EditText android:id="@+id/editusername"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
thank you.谢谢你。
The problem is that this in the Java:问题是这在Java中:
String s = "username="+username+"&password=" + password;
Doesn't match up with this in the PHP:在 PHP 中与此不匹配:
//Getting values
$username = $_POST['editusername'];
$password = $_POST['editpassword'];
Also note that you should use URLEncoder.encode()
for values that come from user input:另请注意,您应该对来自用户输入的值使用
URLEncoder.encode()
:
String charset = "UTF-8";
String s = "editusername=" +
URLEncoder.encode(username, charset) +
"&editpassword=" +
URLEncoder.encode(password, charset);
Also, you can't use ==
with Strings in Java, you need to use the equals()
method:此外,您不能在 Java 中将
==
与字符串一起使用,您需要使用equals()
方法:
@Override
protected void onPostExecute(String s) {
//super.onPostExecute(s);
String ss = s;
loadingDialog.dismiss();
//use equals() instead:
if(ss.equals("success")) {
Intent intent = new Intent(MainActivity.this, UserProfile.class);
intent.putExtra(USER_NAME, username);
finish();
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "invalide username or password", Toast.LENGTH_LONG).show();
}}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.