這是我的android 輸入
傳至loginphp.class檔案
我的資料庫
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'");
?>
                        你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 收到這段值 讓我判定是否可以登入![]()
android要收到值
String result =dblogin.executeQuery(id,password);
 try{
      JSONArray jsonArray = new JSONArray(result);
      }
      catch....
      {
      }
如果你php判斷有這帳號,那result就不會是空的
錯了result=null,就知道無此帳號
神威抱歉請問一下
dblogin.executeQuery(id,password);
請問這個的dblogin來自哪裡?
還有這個是寫在第一張圖的地方沒錯吧??
對
我的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'";
                    求關注
設定上:
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)
                    求關注
你有把libs/httpclient-4.5.7.jar import到你的專案嗎?
我直接寄檔案給你好了,請提供你的mail,謝謝
Android 的語法我不太懂,但是我想你應該要試試看在Android 的部分去接下送到php所回傳的response,然後藉由response 的status code或者data做出判斷。
然後mysql_query/mysql_select_db 這種語法已經被棄用了,我不太知道你php的版本是多少,如果是新的話可能這段語法整個無法執行喔。