iT邦幫忙

0

android php login 登入條件以及接收判斷問題

這是我的android 輸入
https://ithelp.ithome.com.tw/upload/images/20200212/20109685alG6HTUK0T.png
傳至loginphp.class檔案
https://ithelp.ithome.com.tw/upload/images/20200212/20109685doktyKqExo.png
我的資料庫
https://ithelp.ithome.com.tw/upload/images/20200212/20109685U2DanvGX5x.png
php檔案 <- 我想問這個該怎麼改 我想讓使用者輸入帳密這樣然後會成功登入 可是我不知道判斷該怎麼寫 不管是android的或者是php的 我是新手請幫幫我

<?php
require_once("SQL_data.php");
$insert_new=isset($_POST["id"])<>NULL?$_POST["id"]:"null";
$insert_new1=isset($_POST["password"])<>NULL?$_POST["password"]:"null";
mysql_select_db($database,$GD);
$sql = "select * from user where id='$insert_new' and password='$insert_new1'";
$Result=mysql_query($sql,$GD) or die(mysql_error());
$rows=mysql_num_rows($Result);
if($rows){
	$key=true;
	echo "true";
	$respone["success"] = true;
}
else{
	$key=false;
	echo "false";
	$respone["success"] = false;
}
mysql_close();
exit();
?>

這是我連線資料的php

<?php
$hostname="------.byethost.com";
$username="b17_-----";
$password="------";
$database="b1-------ople";
$GD=mysql_pconnect($hostname,$username,$password) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_query("SET NAMES UTF8");
mysql_query("SET CHARACTER_SET_CLIENT='utf8'");
mysql_query("SET CHARACTER_SET_RESULTS='utf8'");
?>
看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2020-02-12 11:19:38 檢舉
新手不是免死金牌

說說你做了哪些 search 跟遇到那些困難吧
謝謝你的回覆 是這樣的我想在手機端這讓使用者輸入帳密傳到php這邊 進行判斷
我目前的疑問是 我寫進php後我要怎麼判斷以及手機這邊譖麼接判斷
dragonH iT邦超人 5 級 ‧ 2020-02-12 11:40:37 檢舉
傳到 php 之後

到 database 比對資料

之後回傳比對結果給手機端
select * from user(id, password) VALUES ('%s','%s')
我好奇的是這個語法會動嗎
我有用這段寫 將資料寫進database 所以因該是可行的
require_once("SQL_data.php");
$insert_new=isset($_POST["S1"])<>NULL?$_POST["S1"]:"null";
$insert_new1=isset($_POST["S2"])<>NULL?$_POST["S2"]:"null";
mysql_select_db($database,$GD);
$sql=sprintf("INSERT INTO user(id, password) VALUES ('%s','%s')",$insert_new,$insert_new1);
$Result=mysql_query($sql,$GD) or die(mysql_error());
mysql_close();
exit();
問題是SELECT 語法我沒看過你那樣用的
select * from user where id = '%s' and password = '%s'
我是看這篇的http://devildemon.pixnet.net/blog/post/216748836-%5Bphp%5D-%E5%B8%B3%E8%99%9F%E5%AF%86%E7%A2%BC%E7%99%BB%E5%85%A5%E8%AA%9E%E6%B3%95
dragonH iT邦超人 5 級 ‧ 2020-02-12 13:10:39 檢舉
可是妳問題中寫的code 是 select ... values
抱歉 我的意思是我目前在參考這篇去改中 只是我php基礎很遭 所以目前一片混亂
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
神威
iT邦研究生 4 級 ‧ 2020-02-12 13:22:48
最佳解答

你android參數傳到PHP後
PHP再進行判斷是否有這組帳號
select 語法試試看吧:

select* from user where id='$id' and password='$password' 
看更多先前的回應...收起先前的回應...

請問各位高手 這樣語法上有問題嗎??

<?php
require_once("SQL_data.php");
$insert_new=isset($_POST["id"])<>NULL?$_POST["id"]:"null";
$insert_new1=isset($_POST["password"])<>NULL?$_POST["password"]:"null";
mysql_select_db($database,$GD);
$sql = "select* from user where id='$id' and password='$password'";
$Result=mysql_query($sql,$GD) or die(mysql_error());
$rows=mysql_num_rows($Result);
if($rows){
	$key=true;
    echo "true";
}
else{
	$key=false;
    echo "false";
}
mysql_close();
exit();
?>

接下來是我想要讓android 收到這段值 讓我判定是否可以登入/images/emoticon/emoticon06.gif

神威 iT邦研究生 4 級 ‧ 2020-02-12 16:09:15 檢舉

android要收到值

String result =dblogin.executeQuery(id,password);
 try{
      JSONArray jsonArray = new JSONArray(result);
      }
      catch....
      {
      }

如果你php判斷有這帳號,那result就不會是空的
錯了result=null,就知道無此帳號

神威抱歉請問一下
dblogin.executeQuery(id,password);
請問這個的dblogin來自哪裡?
還有這個是寫在第一張圖的地方沒錯吧??

神威 iT邦研究生 4 級 ‧ 2020-02-12 16:32:03 檢舉


我的dblogin等於你的loginphp
求關注
直接PO我以前寫的程式碼給你參考:

Login.java:

public class Login extends AppCompatActivity {
    private TextView acc,pwd;
  
    String mycart="",account="",names="",passwd="";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
                .penaltyLog()
                .penaltyDeath()
                .build());
       
        acc=(TextView)findViewById(R.id.acc);
        pwd=(TextView)findViewById(R.id.pwd);
        btn=(ImageView)findViewById(R.id.btn);
        btn.setOnClickListener(btnlogin);

    }

    private ImageView.OnClickListener btnlogin=new ImageView.OnClickListener(){
        public void onClick(View v){
            String account=acc.getText().toString();
            String passwd=pwd.getText().toString();
           
            String result =dblogin.executeQuery(account,passwd);

            try{
                JSONArray jsonArray = new JSONArray(result);

                for(int i = 0; i < jsonArray.length(); i++) 
                {	 JSONObject jsonData = jsonArray.getJSONObject(i);
                    String name=jsonData.getString("name");
                    String email=jsonData.getString("email");
                  
                        Intent intent= new Intent();//轉跳另一頁,並顯示帳密及信箱
                        intent.setClass(Login.this, nextpage.class);
                        Bundle bundle=new Bundle();
                        bundle.putString("ACCOUNT", account);
                        bundle.putString("NAME", name);
                        bundle.putString("EMAIL", email);
                        intent.putExtras(bundle);
                        startActivity(intent);
                   
                }
            }

            catch(Exception e){}
        }
    };

}

dblogin.java


public class dblogin {
    public static String executeQuery(String account,String passwd) {
        String result = ""; 
       
        try {   
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://aaaa.xxxx/login.php");
            ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("account", account));
            params.add(new BasicNameValuePair("passwd",passwd));
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            //view_account.setText(httpResponse.getStatusLine().toString());
            HttpEntity httpEntity = httpResponse.getEntity();
            InputStream inputStream = httpEntity.getContent();
            
            BufferedReader bufReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
            StringBuilder builder = new StringBuilder();
            String line = null;
            while((line = bufReader.readLine()) != null) {
                builder.append(line + "\n");
            }
            inputStream.close();
            result = builder.toString();
        } catch(Exception e) {
            // Log.e("log_tag", e.toString());
        }
        
        return result;
    }
}

php

<?php
require_once("db.php");//db.php就是php連資料庫那支程式,你應該懂吧,我就不放了
$account=$_POST['account'];
$passwd=$_POST['passwd'];
$sql = "select* from user where account= '$account' and passwd='$passwd'";
$q=mysqli_query($db,$sql);
while($e=mysqli_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysqli_close($q);
  
?>

神威 傷心 我沒辦法執行
我有幾個問題請教一下
請問你這樣連不是需要cookie嗎?
Android Main
我將這邊改成EditView
acc=(TextView)findViewById(R.id.acc);
pwd=(TextView)findViewById(R.id.pwd);

dblogin
我匯入的

import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import cz.msebera.android.httpclient.protocol.HTTP;

php的部分 我只改以下

$sql = "select* from user where id= '$account' and password='$passwd'";
神威 iT邦研究生 4 級 ‧ 2020-02-12 18:16:27 檢舉

求關注
設定上:
AndroidManifest.xml

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
    
    //這個加在<application>中
      <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />

另外:
import library
httpclient-4.5.7.jar
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.5.7

確認build.gradle有

android {

   ......
   useLibrary 'org.apache.http.legacy'
    packagingOptions {

    }
}

dependencies {
.....
 implementation files('libs/httpclient-4.5.7.jar')
}

應該就OK了

神威 還是不行請救救我
他標這兩條錯誤 不是很懂這是哪裡錯了
HttpResponse httpResponse = httpClient.execute(httpPost);
String result =dblogin.executeQuery(account,passwd);

 at com.example.login.dblogin.executeQuery(dblogin.java:31)
        at com.example.login.MainActivity$1.onClick(MainActivity.java:50)
神威 iT邦研究生 4 級 ‧ 2020-02-13 08:57:12 檢舉

求關注
你有把libs/httpclient-4.5.7.jar import到你的專案嗎?

我直接寄檔案給你好了,請提供你的mail,謝謝

0
r567tw
iT邦研究生 5 級 ‧ 2020-02-12 12:29:22

Android 的語法我不太懂,但是我想你應該要試試看在Android 的部分去接下送到php所回傳的response,然後藉由response 的status code或者data做出判斷。

然後mysql_query/mysql_select_db 這種語法已經被棄用了,我不太知道你php的版本是多少,如果是新的話可能這段語法整個無法執行喔。

我是舊的 我確定我對資料庫資料新增刪除修改都行 但我帳號這邊真的搞不出來 傷心

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-02-13 10:56:29

Android / PHP / MySQL 登入實作
這篇試試看
比較接近你要做的東西

我要發表回答

立即登入回答