Android - 登入系統:創建會員


 先在MYSQL建立會員用的表單和之後需要的欄位

Android端:假設MYSQL建立了5個欄位(帳號、密碼、姓名、信箱、電話)

  1. public class register extends AppCompatActivity {/**創建帳號用**/
  2. private EditText editTextName;
  3. private EditText editTextUsername;
  4. private EditText editTextPassword;
  5. private EditText editTextEmail;
  6. private EditText editTextTel;
  7. private Button buttonRegister;
  8. private String REGISTER_URL;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_register);
  13. REGISTER_URL = getString(R.string.register_url);
  14. editTextName = (EditText) findViewById(R.id.name);
  15. editTextUsername = (EditText) findViewById(R.id.username);
  16. editTextPassword = (EditText) findViewById(R.id.password);
  17. editTextEmail = (EditText) findViewById(R.id.email);
  18. editTextTel= (EditText) findViewById(R.id.tel);
  19. buttonRegister = (Button) findViewById(R.id.create);
  20. buttonRegister.setOnClickListener(buttonListener);
  21. }
  22. private Button.OnClickListener buttonListener = new Button.OnClickListener() {/**監聽創見帳號鈕是否被按下**/
  23. @Override
  24. public void onClick(View v) {
  25. if(v == buttonRegister){
  26. registerUser();/**呼叫這函式進行使用者資料獲取**/
  27. }
  28. }
  29. };
  30. private void registerUser() {/**讀取使用者輸入數據**/
  31. String name = editTextName.getText().toString().trim().toLowerCase();
  32. String username = editTextUsername.getText().toString().trim().toLowerCase();
  33. String password = editTextPassword.getText().toString().trim().toLowerCase();
  34. String email = editTextEmail.getText().toString().trim().toLowerCase();
  35. String tel = editTextTel.getText().toString().trim().toLowerCase();
  36. register(name,username,password,email,tel);/**獲取資料成功後,開始進行傳送**/
  37. }
  38. private void register(String name, String username, String password, String email, String tel) {
  39. class RegisterUser extends AsyncTask<String, Void, String> {
  40. Createmem ruc = new Createmem();/**使用Creatmem.class的功能**/
  41. @Override
  42. protected void onPreExecute()
  43. {
  44. super.onPreExecute();/**當按下創見鈕,出現提式窗**/
  45. }
  46. @Override
  47. protected void onPostExecute(String s) {
  48. super.onPostExecute(s);
  49. Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
  50. if(s.equals("帳號創建成功!"))/**當字串比對成功返回登入頁面**/
  51. {
  52. Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
  53. Intent intent = new Intent();
  54. intent.setClass(register.this,login.class);
  55. startActivity(intent);
  56. finish();
  57. }
  58. }
  59. @Override
  60. protected String doInBackground(String... params)/**將資料放入hashmap**/
  61. {
  62. HashMap<String, String> data = new HashMap<String,String>();
  63. data.put("name",params[0]);
  64. data.put("password",params[1]);
  65. data.put("username",params[2]);
  66. data.put("email",params[3]);
  67. data.put("tel",params[4]);
  68. String result = ruc.sendPostRequest(REGISTER_URL,data);
  69. return result;
  70. }
  71. }
  72. RegisterUser ru = new RegisterUser();/**傳送資料**/
  73. ru.execute(name, password, username, email,tel);
  74. }
  75. }
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.register">
  8.  
  9. <ImageView android:id="@+id/image"
  10. android:background="@drawable/newmem"
  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. <EditText 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:hint="Account"
  37. android:inputType="text"/>
  38.  
  39. <View android:background="#ffc0c3c4"
  40. android:layout_width="fill_parent"
  41. android:layout_height="1.0px"
  42. android:layout_marginLeft="1.0px"
  43. android:layout_marginRight="1.0px" />
  44.  
  45. <EditText android:textSize="16.0sp"
  46. android:textColor="#E0E0E0"
  47. android:textColorHint="#E0E0E0"
  48. android:hint="Password"
  49. android:gravity="center_vertical"
  50. android:id="@+id/password"
  51. android:background="#40000000"
  52. android:paddingLeft="12.0dip"
  53. android:layout_width="fill_parent"
  54. android:layout_height="44dp"
  55. android:maxLines="1"
  56. android:maxLength="20"
  57. android:inputType="textVisiblePassword" />
  58.  
  59. <View android:background="#ffc0c3c4"
  60. android:layout_width="fill_parent"
  61. android:layout_height="1.0px"
  62. android:layout_marginLeft="1.0px"
  63. android:layout_marginRight="1.0px" />
  64.  
  65. <EditText android:textSize="16.0sp"
  66. android:textColor="#E0E0E0"
  67. android:textColorHint="#E0E0E0"
  68. android:hint="Name"
  69. android:gravity="center_vertical"
  70. android:id="@+id/username"
  71. android:background="#40000000"
  72. android:paddingLeft="12.0dip"
  73. android:layout_width="fill_parent"
  74. android:layout_height="44dp"
  75. android:maxLines="1"
  76. android:maxLength="20"
  77. android:inputType="textPersonName" />
  78.  
  79. <View android:background="#ffc0c3c4"
  80. android:layout_width="fill_parent"
  81. android:layout_height="1.0px"
  82. android:layout_marginLeft="1.0px"
  83. android:layout_marginRight="1.0px" />
  84.  
  85. <EditText android:textSize="16.0sp"
  86. android:textColor="#E0E0E0"
  87. android:textColorHint="#E0E0E0"
  88. android:hint="Email"
  89. android:gravity="center_vertical"
  90. android:id="@+id/email"
  91. android:background="#40000000"
  92. android:paddingLeft="12.0dip"
  93. android:layout_width="fill_parent"
  94. android:layout_height="44dp"
  95. android:maxLines="1"
  96. android:maxLength="30"
  97. android:inputType="textEmailAddress" />
  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. <EditText android:textSize="16.0sp"
  105. android:textColor="#E0E0E0"
  106. android:textColorHint="#E0E0E0"
  107. android:hint="Telephone"
  108. android:gravity="center_vertical"
  109. android:id="@+id/tel"
  110. android:background="#40000000"
  111. android:paddingLeft="12.0dip"
  112. android:layout_width="fill_parent"
  113. android:layout_height="44dp"
  114. android:maxLines="1"
  115. android:maxLength="10"
  116. android:inputType="phone" />
  117.  
  118. <Button
  119. android:layout_width="match_parent"
  120. android:layout_height="match_parent"
  121. android:gravity="center"
  122. android:background="#40000000"
  123. android:text="Create Account"
  124. android:textStyle="bold"
  125. android:id="@+id/create"
  126. android:paddingTop="5.0dip"
  127. android:layout_marginLeft="12.0dip"
  128. android:layout_marginTop="12.0dip"
  129. android:layout_marginRight="12.0dip"
  130. android:textSize="20.0sp"
  131. />
  132. </LinearLayout>
  133. </RelativeLayout>
Server端創建會員PHP
  1. <?php
  2. /**用於使用者創建帳號**/
  3. if($_SERVER['REQUEST_METHOD']=='POST'){//限制條件為POST
  4. $name = $_POST['name'];//將使用者傳的資料存進變數
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7. $email = $_POST['email'];
  8. $tel = $_POST['tel'];
  9. if($name == '' || $username == '' || $password == '' || $email == '' || $tel == ''){
  10. echo '請填滿所有選項!';
  11. }
  12. else
  13. {
  14. require_once('dbConnect.php');
  15. $sql = "SELECT * FROM userinfo WHERE name='$name'";
  16. $check = mysqli_fetch_array(mysqli_query($con,$sql));
  17. if(isset($check))
  18. {
  19. echo '此帳號已經存在!';
  20. }
  21. else
  22. {
  23. $sql = "INSERT INTO userinfo (name,password,username,email,tel) VALUES('$name','$password','$username','$email','$tel')";
  24. if(mysqli_query($con,$sql))
  25. {
  26. echo '帳號創建成功!';
  27. }
  28. else
  29. {
  30. echo '請再嘗試一次!';
  31. }
  32. }
  33. mysqli_close($con);
  34. }
  35. }
  36. else
  37. {
  38. echo 'Error';
  39. }
  40. ?>

留言

張貼留言