Android - 登入系統:會員資料修改


會員資料修改,判斷使用者輸入那些欄位,針對該欄位修改對應的資料表欄位

Android端
  1. /**修改會員資料**/
  2. public class set extends AppCompatActivity {
  3. String id;
  4. private EditText editTextUsername;
  5. private EditText editTextPassword;
  6. private EditText editTextEmail;
  7. private EditText editTextTel;
  8. private Button buttonchange;
  9. private String SET_URL;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_set);
  14. SET_URL = getString(R.string.set_url);
  15. Bundle bundle = this.getIntent().getExtras();/**接收首頁傳來的user ID**/
  16. id = bundle.getString("id");
  17. editTextUsername = (EditText) findViewById(R.id.username);
  18. editTextPassword = (EditText) findViewById(R.id.password);
  19. editTextEmail = (EditText) findViewById(R.id.email);
  20. editTextTel= (EditText) findViewById(R.id.tel);
  21. buttonchange = (Button) findViewById(R.id.create);
  22. buttonchange.setOnClickListener(buttonListener);
  23. }
  24.  
  25. private Button.OnClickListener buttonListener = new Button.OnClickListener() {/**監聽修改鈕是否被按下**/
  26. @Override
  27. public void onClick(View v) {
  28.  
  29. if(v == buttonchange){
  30. checkDialog();
  31. }
  32. }
  33. };
  34. private void checkDialog()
  35. {
  36. Dialog dialog=new AlertDialog.Builder(this).setTitle("修改會員資料").setMessage("確定要修改嗎?").setNegativeButton("Yes", new OnClickListener()
  37. {
  38. public void onClick(DialogInterface dialog, int which)
  39. {
  40. changeinfoget();/**呼叫這函式進行使用者資料獲取**/
  41. }
  42. }).setPositiveButton("Cancel", new OnClickListener()
  43. {
  44. public void onClick(DialogInterface dialog, int which) {
  45. // TODO Auto-generated method stub
  46. }
  47. }).create();/**創建Dialog**/
  48. dialog.show();/**顯示對話框**/
  49. }
  50.  
  51. private void changeinfoget() {/**讀取使用者輸入數據**/
  52. String name = id.trim().toLowerCase();
  53. String username = editTextUsername.getText().toString().trim().toLowerCase();
  54. String password = editTextPassword.getText().toString().trim().toLowerCase();
  55. String email = editTextEmail.getText().toString().trim().toLowerCase();
  56. String tel = editTextTel.getText().toString().trim().toLowerCase();
  57. change(name, username, password, email, tel);/**獲取資料成功後,開始進行傳送**/
  58. }
  59. private void change(String name,String username, String password, String email, String tel) {
  60. class RegisterUser extends AsyncTask<String, Void, String> {
  61. Createmem ruc = new Createmem();/**使用Creatmem.class的功能**/
  62. @Override
  63. protected void onPreExecute()
  64. {
  65. super.onPreExecute();/**當按下修改鈕,出現提式窗**/
  66. }
  67. @Override
  68. protected void onPostExecute(String s) {
  69. super.onPostExecute(s);
  70. String[] splitans = s.split(",");
  71. Toast.makeText(getApplicationContext(), splitans[0], Toast.LENGTH_SHORT).show();
  72. }
  73. @Override
  74. protected String doInBackground(String... params)/**將資料放入hashmap**/
  75. {
  76. HashMap<String, String> data = new HashMap<String,String>();
  77. data.put("name",params[0]);
  78. data.put("username",params[1]);
  79. data.put("password",params[2]);
  80. data.put("email",params[3]);
  81. data.put("tel",params[4]);
  82. String result = ruc.sendPostRequest(SET_URL,data);
  83. return result;
  84. }
  85. }
  86. RegisterUser ru = new RegisterUser();/**傳送資料**/
  87. ru.execute(name ,username, password, email, tel);
  88. }
  89. }
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.set">
  8.  
  9. <ImageView android:id="@+id/image"
  10. android:background="@drawable/changeinfo"
  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.  
  27. <EditText android:textSize="16.0sp"
  28. android:textColor="#E0E0E0"
  29. android:textColorHint="#E0E0E0"
  30. android:hint="New Name"
  31. android:gravity="center_vertical"
  32. android:id="@+id/username"
  33. android:background="#40000000"
  34. android:paddingLeft="12.0dip"
  35. android:textStyle="bold"
  36. android:layout_width="fill_parent"
  37. android:layout_height="44dp"
  38. android:maxLines="1"
  39. android:maxLength="20"
  40. android:inputType="textPersonName" />
  41.  
  42. <View android:background="#ffc0c3c4"
  43. android:layout_width="fill_parent"
  44. android:layout_height="1.0px"
  45. android:layout_marginLeft="1.0px"
  46. android:layout_marginRight="1.0px" />
  47.  
  48. <EditText android:textSize="16.0sp"
  49. android:textColor="#E0E0E0"
  50. android:textColorHint="#E0E0E0"
  51. android:hint="New Password"
  52. android:gravity="center_vertical"
  53. android:id="@+id/password"
  54. android:background="#40000000"
  55. android:paddingLeft="12.0dip"
  56. android:textStyle="bold"
  57. android:layout_width="fill_parent"
  58. android:layout_height="44dp"
  59. android:maxLines="1"
  60. android:maxLength="20"
  61. android:inputType="textVisiblePassword" />
  62.  
  63. <View android:background="#ffc0c3c4"
  64. android:layout_width="fill_parent"
  65. android:layout_height="1.0px"
  66. android:layout_marginLeft="1.0px"
  67. android:layout_marginRight="1.0px" />
  68.  
  69. <EditText android:textSize="16.0sp"
  70. android:textColor="#E0E0E0"
  71. android:textColorHint="#E0E0E0"
  72. android:hint="New Email"
  73. android:gravity="center_vertical"
  74. android:id="@+id/email"
  75. android:textStyle="bold"
  76. android:background="#40000000"
  77. android:paddingLeft="12.0dip"
  78. android:layout_width="fill_parent"
  79. android:layout_height="44dp"
  80. android:maxLines="1"
  81. android:maxLength="30"
  82. android:inputType="textEmailAddress" />
  83.  
  84. <View android:background="#ffc0c3c4"
  85. android:layout_width="fill_parent"
  86. android:layout_height="1.0px"
  87. android:layout_marginLeft="1.0px"
  88. android:layout_marginRight="1.0px" />
  89. <EditText android:textSize="16.0sp"
  90. android:textColor="#E0E0E0"
  91. android:textColorHint="#E0E0E0"
  92. android:hint="New Telephone"
  93. android:gravity="center_vertical"
  94. android:textStyle="bold"
  95. android:id="@+id/tel"
  96. android:background="#40000000"
  97. android:paddingLeft="12.0dip"
  98. android:layout_width="fill_parent"
  99. android:layout_height="44dp"
  100. android:maxLines="1"
  101. android:maxLength="10"
  102. android:inputType="phone" />
  103.  
  104. <Button
  105. android:layout_width="match_parent"
  106. android:layout_height="match_parent"
  107. android:gravity="center"
  108. android:background="#40000000"
  109. android:text="Change Settings"
  110. android:textStyle="bold"
  111. android:id="@+id/create"
  112. android:paddingTop="5.0dip"
  113. android:layout_marginLeft="12.0dip"
  114. android:layout_marginTop="12.0dip"
  115. android:layout_marginRight="12.0dip"
  116. android:textSize="20.0sp"
  117. />
  118. </LinearLayout>
  119. </RelativeLayout>
  120.  
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($username == '' && $password == '' && $email == '' && $tel == ''){
  10. echo '沒有項目被修改!';
  11. echo ',';
  12. }
  13. if($username != '')
  14. {
  15. require_once('dbConnect.php');
  16. $sql = "SELECT * FROM userinfo WHERE name='$name'";
  17. $check = mysqli_fetch_array(mysqli_query($con,$sql));
  18. if(isset($check))
  19. {
  20. $sql_2 = "UPDATE userinfo SET username = '$username' WHERE name = '$name'";
  21. if(mysqli_query($con,$sql_2))
  22. {
  23. echo '修改成功!';
  24. echo ',';
  25. }
  26. else
  27. {
  28. echo '請再嘗試一次!';
  29. }
  30. }
  31. }
  32. if($password != '')
  33. {
  34. require_once('dbConnect.php');
  35. $sql_3 = "SELECT * FROM userinfo WHERE name='$name'";
  36. $check = mysqli_fetch_array(mysqli_query($con,$sql_3));
  37. if(isset($check))
  38. {
  39. $sql_4 = "UPDATE userinfo SET password = '$password' WHERE name = '$name'";
  40. if(mysqli_query($con,$sql_4))
  41. {
  42. echo '修改成功!';
  43. echo ',';
  44. }
  45. else
  46. {
  47. echo '請再嘗試一次!';
  48. }
  49. }
  50. }
  51. if($email != '')
  52. {
  53. require_once('dbConnect.php');
  54. $sql_5 = "SELECT * FROM userinfo WHERE name='$name'";
  55. $check = mysqli_fetch_array(mysqli_query($con,$sql_5));
  56. if(isset($check))
  57. {
  58. $sql_6 = "UPDATE userinfo SET email = '$email' WHERE name = '$name'";
  59. if(mysqli_query($con,$sql_6))
  60. {
  61. echo '修改成功!';
  62. echo ',';
  63. }
  64. else
  65. {
  66. echo '請再嘗試一次!';
  67. }
  68. }
  69. }
  70. if($tel != '')
  71. {
  72. require_once('dbConnect.php');
  73. $sql_7 = "SELECT * FROM userinfo WHERE name='$name'";
  74. $check = mysqli_fetch_array(mysqli_query($con,$sql_7));
  75. if(isset($check))
  76. {
  77. $sql_8 = "UPDATE userinfo SET tel = '$tel' WHERE name = '$name'";
  78. if(mysqli_query($con,$sql_8))
  79. {
  80. echo '修改成功!';
  81. echo ',';
  82. }
  83. else
  84. {
  85. echo '請再嘗試一次!';
  86. }
  87. }
  88. }
  89. mysqli_close($con);
  90. }
  91. else
  92. {
  93. echo 'Error';
  94. }
  95. ?>

留言