Android - 登入系統:查詢使用者資訊


透過唯一的ID進行資料庫搜尋,並回傳顯示對應的搜尋結果
 Android端
/**查詢會員資訊**/
public class userinfo extends AppCompatActivity {
    String id;
    private TextView name,username,email,tel,ip;
    private String SEARCH_URL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);
        SEARCH_URL = getString(R.string.userinfo_url);
        Bundle bundle = this.getIntent().getExtras();/**接收首頁傳來的user ID**/
        id = bundle.getString("id");

        name = (TextView)findViewById(R.id.name);
        username = (TextView)findViewById(R.id.username);
        email = (TextView)findViewById(R.id.email);
        tel = (TextView)findViewById(R.id.tel);
        ip = (TextView)findViewById(R.id.ip);
        searchset(id);/**搜尋前的前置作業**/
    }

    private void searchset(String id) {/**將獲得的帳號準備拿去資料庫比對搜尋**/
        String useraccount = id.trim().toLowerCase();
        search(useraccount);/**呼叫search開始傳送**/
    }

    private void search(String useraccount) {
        class RegisterUser extends AsyncTask<String, Void, String> {
            Createmem ruc = new Createmem();/**使用http的功能**/
            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();/**當按下查詢鈕,出現提式窗**/
            }
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                String[] splitans = s.split(",");
                name.setText("Account  :  "+id);
                username.setText("Name  :  "+splitans[0]);/**設定顯示**/
                email.setText("Email  :  "+splitans[1]);
                tel.setText("Telephone  :  "+splitans[2]);
                ip.setText("IP Address  :  "+splitans[3]);
            }
            @Override
            protected String doInBackground(String... params)/**將資料放入hashmap**/
            {
                HashMap<String, String> data = new HashMap<String,String>();
                data.put("name", params[0]);
                String result = ruc.sendPostRequest(SEARCH_URL,data);
                return  result;
            }
        }
        RegisterUser ru = new RegisterUser();/**傳送資料**/
        ru.execute(useraccount);
    }
}
Android介面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_bg"
    tools:context="project.rmotex.achat.userinfo">

    <ImageView android:id="@+id/image"
        android:background="@drawable/userinfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="28.0dip"
        android:layout_marginRight="28.0dip"
        android:layout_below="@+id/image">

        <TextView android:textSize="16.0sp"
            android:textColor="#E0E0E0"
            android:textColorHint="#E0E0E0"
            android:id="@+id/name"
            android:background="#40000000"
            android:paddingLeft="12.0dip"
            android:layout_width="fill_parent"
            android:layout_height="44dp"
            android:maxLines="1"
            android:maxLength="20"
            android:textStyle="bold"
            android:gravity="center_vertical"
            />

        <View android:background="#ffc0c3c4"
            android:layout_width="fill_parent"
            android:layout_height="1.0px"
            android:layout_marginLeft="1.0px"
            android:layout_marginRight="1.0px" />

        <TextView android:textSize="16.0sp"
            android:textColor="#E0E0E0"
            android:textColorHint="#E0E0E0"
            android:gravity="center_vertical"
            android:id="@+id/username"
            android:background="#40000000"
            android:paddingLeft="12.0dip"
            android:layout_width="fill_parent"
            android:layout_height="44dp"
            android:textStyle="bold"
            android:maxLines="1"
            android:maxLength="20"
             />

        <View android:background="#ffc0c3c4"
            android:layout_width="fill_parent"
            android:layout_height="1.0px"
            android:layout_marginLeft="1.0px"
            android:layout_marginRight="1.0px" />

        <TextView android:textSize="16.0sp"
            android:textColor="#E0E0E0"
            android:textColorHint="#E0E0E0"
            android:gravity="center_vertical"
            android:id="@+id/email"
            android:background="#40000000"
            android:paddingLeft="12.0dip"
            android:layout_width="fill_parent"
            android:layout_height="44dp"
            android:textStyle="bold"
            android:maxLines="1"
            android:maxLength="30"
            />

        <View android:background="#ffc0c3c4"
        android:layout_width="fill_parent"
        android:layout_height="1.0px"
        android:layout_marginLeft="1.0px"
        android:layout_marginRight="1.0px" />

        <TextView android:textSize="16.0sp"
            android:textColor="#E0E0E0"
            android:textColorHint="#E0E0E0"
            android:gravity="center_vertical"
            android:id="@+id/tel"
            android:background="#40000000"
            android:paddingLeft="12.0dip"
            android:layout_width="fill_parent"
            android:layout_height="44dp"
            android:textStyle="bold"
            android:maxLines="1"
            android:maxLength="30"
            />
        <View android:background="#ffc0c3c4"
            android:layout_width="fill_parent"
            android:layout_height="1.0px"
            android:layout_marginLeft="1.0px"
            android:layout_marginRight="1.0px" />

        <TextView android:textSize="16.0sp"
            android:textColor="#E0E0E0"
            android:textColorHint="#E0E0E0"
            android:gravity="center_vertical"
            android:id="@+id/ip"
            android:background="#40000000"
            android:paddingLeft="12.0dip"
            android:layout_width="fill_parent"
            android:layout_height="44dp"
            android:textStyle="bold"
            android:maxLines="1"
            android:maxLength="30"
            />
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#40000000"
        android:text="個 人 訊 息 查 閱"
        android:textStyle="bold"
        android:id="@+id/send"
        android:textSize="20.0sp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
 Server端查詢PHP
<?php
/**用於查詢會員資料**/
if($_SERVER['REQUEST_METHOD']=='POST'){//限制條件為POST
 $name = $_POST['name'];

 require_once('dbConnect.php');
 $sql = "SELECT * FROM userinfo WHERE name='$name'";
 $checkk = mysqli_fetch_array(mysqli_query($con,$sql));
 if(isset($checkk)== true)
 {
 $sqll ="SELECT username,email,tel FROM userinfo WHERE name='$name'";
 $result = $con->query($sqll);
 if($result->num_rows >0)
 {
  while($row = $result->fetch_assoc())
  {
   echo $row['username'];
   echo ',';
   echo $row['email'];
   echo ',';
   echo $row['tel'];
  }
 }
 }
 else
 {
 echo '查詢失敗!';
 }
 mysqli_close($con);
}
else
{
 echo 'Error';
}
?>

留言