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


透過唯一的ID進行資料庫搜尋,並回傳顯示對應的搜尋結果
 Android端
  1. /**查詢會員資訊**/
  2. public class userinfo extends AppCompatActivity {
  3. String id;
  4. private TextView name,username,email,tel,ip;
  5. private String SEARCH_URL;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_userinfo);
  10. SEARCH_URL = getString(R.string.userinfo_url);
  11. Bundle bundle = this.getIntent().getExtras();/**接收首頁傳來的user ID**/
  12. id = bundle.getString("id");
  13.  
  14. name = (TextView)findViewById(R.id.name);
  15. username = (TextView)findViewById(R.id.username);
  16. email = (TextView)findViewById(R.id.email);
  17. tel = (TextView)findViewById(R.id.tel);
  18. ip = (TextView)findViewById(R.id.ip);
  19. searchset(id);/**搜尋前的前置作業**/
  20. }
  21.  
  22. private void searchset(String id) {/**將獲得的帳號準備拿去資料庫比對搜尋**/
  23. String useraccount = id.trim().toLowerCase();
  24. search(useraccount);/**呼叫search開始傳送**/
  25. }
  26.  
  27. private void search(String useraccount) {
  28. class RegisterUser extends AsyncTask<String, Void, String> {
  29. Createmem ruc = new Createmem();/**使用http的功能**/
  30. @Override
  31. protected void onPreExecute()
  32. {
  33. super.onPreExecute();/**當按下查詢鈕,出現提式窗**/
  34. }
  35. @Override
  36. protected void onPostExecute(String s) {
  37. super.onPostExecute(s);
  38. String[] splitans = s.split(",");
  39. name.setText("Account : "+id);
  40. username.setText("Name : "+splitans[0]);/**設定顯示**/
  41. email.setText("Email : "+splitans[1]);
  42. tel.setText("Telephone : "+splitans[2]);
  43. ip.setText("IP Address : "+splitans[3]);
  44. }
  45. @Override
  46. protected String doInBackground(String... params)/**將資料放入hashmap**/
  47. {
  48. HashMap<String, String> data = new HashMap<String,String>();
  49. data.put("name", params[0]);
  50. String result = ruc.sendPostRequest(SEARCH_URL,data);
  51. return result;
  52. }
  53. }
  54. RegisterUser ru = new RegisterUser();/**傳送資料**/
  55. ru.execute(useraccount);
  56. }
  57. }
Android介面布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@drawable/login_bg"
  7. tools:context="project.rmotex.achat.userinfo">
  8.  
  9. <ImageView android:id="@+id/image"
  10. android:background="@drawable/userinfo"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="70dp"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerHorizontal="true" />
  16.  
  17. <LinearLayout
  18. android:orientation="vertical"
  19. android:id="@+id/input"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_marginLeft="28.0dip"
  23. android:layout_marginRight="28.0dip"
  24. android:layout_below="@+id/image">
  25.  
  26. <TextView android:textSize="16.0sp"
  27. android:textColor="#E0E0E0"
  28. android:textColorHint="#E0E0E0"
  29. android:id="@+id/name"
  30. android:background="#40000000"
  31. android:paddingLeft="12.0dip"
  32. android:layout_width="fill_parent"
  33. android:layout_height="44dp"
  34. android:maxLines="1"
  35. android:maxLength="20"
  36. android:textStyle="bold"
  37. android:gravity="center_vertical"
  38. />
  39.  
  40. <View android:background="#ffc0c3c4"
  41. android:layout_width="fill_parent"
  42. android:layout_height="1.0px"
  43. android:layout_marginLeft="1.0px"
  44. android:layout_marginRight="1.0px" />
  45.  
  46. <TextView android:textSize="16.0sp"
  47. android:textColor="#E0E0E0"
  48. android:textColorHint="#E0E0E0"
  49. android:gravity="center_vertical"
  50. android:id="@+id/username"
  51. android:background="#40000000"
  52. android:paddingLeft="12.0dip"
  53. android:layout_width="fill_parent"
  54. android:layout_height="44dp"
  55. android:textStyle="bold"
  56. android:maxLines="1"
  57. android:maxLength="20"
  58. />
  59.  
  60. <View android:background="#ffc0c3c4"
  61. android:layout_width="fill_parent"
  62. android:layout_height="1.0px"
  63. android:layout_marginLeft="1.0px"
  64. android:layout_marginRight="1.0px" />
  65.  
  66. <TextView android:textSize="16.0sp"
  67. android:textColor="#E0E0E0"
  68. android:textColorHint="#E0E0E0"
  69. android:gravity="center_vertical"
  70. android:id="@+id/email"
  71. android:background="#40000000"
  72. android:paddingLeft="12.0dip"
  73. android:layout_width="fill_parent"
  74. android:layout_height="44dp"
  75. android:textStyle="bold"
  76. android:maxLines="1"
  77. android:maxLength="30"
  78. />
  79.  
  80. <View android:background="#ffc0c3c4"
  81. android:layout_width="fill_parent"
  82. android:layout_height="1.0px"
  83. android:layout_marginLeft="1.0px"
  84. android:layout_marginRight="1.0px" />
  85.  
  86. <TextView android:textSize="16.0sp"
  87. android:textColor="#E0E0E0"
  88. android:textColorHint="#E0E0E0"
  89. android:gravity="center_vertical"
  90. android:id="@+id/tel"
  91. android:background="#40000000"
  92. android:paddingLeft="12.0dip"
  93. android:layout_width="fill_parent"
  94. android:layout_height="44dp"
  95. android:textStyle="bold"
  96. android:maxLines="1"
  97. android:maxLength="30"
  98. />
  99. <View android:background="#ffc0c3c4"
  100. android:layout_width="fill_parent"
  101. android:layout_height="1.0px"
  102. android:layout_marginLeft="1.0px"
  103. android:layout_marginRight="1.0px" />
  104.  
  105. <TextView android:textSize="16.0sp"
  106. android:textColor="#E0E0E0"
  107. android:textColorHint="#E0E0E0"
  108. android:gravity="center_vertical"
  109. android:id="@+id/ip"
  110. android:background="#40000000"
  111. android:paddingLeft="12.0dip"
  112. android:layout_width="fill_parent"
  113. android:layout_height="44dp"
  114. android:textStyle="bold"
  115. android:maxLines="1"
  116. android:maxLength="30"
  117. />
  118. </LinearLayout>
  119. <Button
  120. android:layout_width="match_parent"
  121. android:layout_height="wrap_content"
  122. android:gravity="center"
  123. android:background="#40000000"
  124. android:text="個 人 訊 息 查 閱"
  125. android:textStyle="bold"
  126. android:id="@+id/send"
  127. android:textSize="20.0sp"
  128. android:layout_alignParentBottom="true"
  129. android:layout_centerHorizontal="true" />
  130. </RelativeLayout>
 Server端查詢PHP
  1. <?php
  2. /**用於查詢會員資料**/
  3. if($_SERVER['REQUEST_METHOD']=='POST'){//限制條件為POST
  4. $name = $_POST['name'];
  5.  
  6. require_once('dbConnect.php');
  7. $sql = "SELECT * FROM userinfo WHERE name='$name'";
  8. $checkk = mysqli_fetch_array(mysqli_query($con,$sql));
  9. if(isset($checkk)== true)
  10. {
  11. $sqll ="SELECT username,email,tel FROM userinfo WHERE name='$name'";
  12. $result = $con->query($sqll);
  13. if($result->num_rows >0)
  14. {
  15. while($row = $result->fetch_assoc())
  16. {
  17. echo $row['username'];
  18. echo ',';
  19. echo $row['email'];
  20. echo ',';
  21. echo $row['tel'];
  22. }
  23. }
  24. }
  25. else
  26. {
  27. echo '查詢失敗!';
  28. }
  29. mysqli_close($con);
  30. }
  31. else
  32. {
  33. echo 'Error';
  34. }
  35. ?>

留言