Android - 登入系統:會員資料修改
會員資料修改,判斷使用者輸入那些欄位,針對該欄位修改對應的資料表欄位
Android端
/**修改會員資料**/ public class set extends AppCompatActivity { String id; private EditText editTextUsername; private EditText editTextPassword; private EditText editTextEmail; private EditText editTextTel; private Button buttonchange; private String SET_URL; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_set); SET_URL = getString(R.string.set_url); Bundle bundle = this.getIntent().getExtras();/**接收首頁傳來的user ID**/ id = bundle.getString("id"); editTextUsername = (EditText) findViewById(R.id.username); editTextPassword = (EditText) findViewById(R.id.password); editTextEmail = (EditText) findViewById(R.id.email); editTextTel= (EditText) findViewById(R.id.tel); buttonchange = (Button) findViewById(R.id.create); buttonchange.setOnClickListener(buttonListener); } private Button.OnClickListener buttonListener = new Button.OnClickListener() {/**監聽修改鈕是否被按下**/ @Override public void onClick(View v) { if(v == buttonchange){ checkDialog(); } } }; private void checkDialog() { Dialog dialog=new AlertDialog.Builder(this).setTitle("修改會員資料").setMessage("確定要修改嗎?").setNegativeButton("Yes", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { changeinfoget();/**呼叫這函式進行使用者資料獲取**/ } }).setPositiveButton("Cancel", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create();/**創建Dialog**/ dialog.show();/**顯示對話框**/ } private void changeinfoget() {/**讀取使用者輸入數據**/ String name = id.trim().toLowerCase(); String username = editTextUsername.getText().toString().trim().toLowerCase(); String password = editTextPassword.getText().toString().trim().toLowerCase(); String email = editTextEmail.getText().toString().trim().toLowerCase(); String tel = editTextTel.getText().toString().trim().toLowerCase(); change(name, username, password, email, tel);/**獲取資料成功後,開始進行傳送**/ } private void change(String name,String username, String password, String email, String tel) { class RegisterUser extends AsyncTask<String, Void, String> { Createmem ruc = new Createmem();/**使用Creatmem.class的功能**/ @Override protected void onPreExecute() { super.onPreExecute();/**當按下修改鈕,出現提式窗**/ } @Override protected void onPostExecute(String s) { super.onPostExecute(s); String[] splitans = s.split(","); Toast.makeText(getApplicationContext(), splitans[0], Toast.LENGTH_SHORT).show(); } @Override protected String doInBackground(String... params)/**將資料放入hashmap**/ { HashMap<String, String> data = new HashMap<String,String>(); data.put("name",params[0]); data.put("username",params[1]); data.put("password",params[2]); data.put("email",params[3]); data.put("tel",params[4]); String result = ruc.sendPostRequest(SET_URL,data); return result; } } RegisterUser ru = new RegisterUser();/**傳送資料**/ ru.execute(name ,username, password, email, tel); } }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.set"> <ImageView android:id="@+id/image" android:background="@drawable/changeinfo" 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"> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="New Name" android:gravity="center_vertical" android:id="@+id/username" android:background="#40000000" android:paddingLeft="12.0dip" android:textStyle="bold" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="20" android:inputType="textPersonName" /> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="New Password" android:gravity="center_vertical" android:id="@+id/password" android:background="#40000000" android:paddingLeft="12.0dip" android:textStyle="bold" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="20" android:inputType="textVisiblePassword" /> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="New Email" android:gravity="center_vertical" android:id="@+id/email" android:textStyle="bold" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="30" android:inputType="textEmailAddress" /> <View android:background="#ffc0c3c4" android:layout_width="fill_parent" android:layout_height="1.0px" android:layout_marginLeft="1.0px" android:layout_marginRight="1.0px" /> <EditText android:textSize="16.0sp" android:textColor="#E0E0E0" android:textColorHint="#E0E0E0" android:hint="New Telephone" android:gravity="center_vertical" android:textStyle="bold" android:id="@+id/tel" android:background="#40000000" android:paddingLeft="12.0dip" android:layout_width="fill_parent" android:layout_height="44dp" android:maxLines="1" android:maxLength="10" android:inputType="phone" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#40000000" android:text="Change Settings" android:textStyle="bold" android:id="@+id/create" android:paddingTop="5.0dip" android:layout_marginLeft="12.0dip" android:layout_marginTop="12.0dip" android:layout_marginRight="12.0dip" android:textSize="20.0sp" /> </LinearLayout> </RelativeLayout>Server端修改資料PHP
<?php /**用於使用者創修改帳號**/ if($_SERVER['REQUEST_METHOD']=='POST'){//限制條件為POST $name = $_POST['name']; $username = $_POST['username']; $password = $_POST['password']; $email = $_POST['email']; $tel = $_POST['tel']; if($username == '' && $password == '' && $email == '' && $tel == ''){ echo '沒有項目被修改!'; echo ','; } if($username != '') { require_once('dbConnect.php'); $sql = "SELECT * FROM userinfo WHERE name='$name'"; $check = mysqli_fetch_array(mysqli_query($con,$sql)); if(isset($check)) { $sql_2 = "UPDATE userinfo SET username = '$username' WHERE name = '$name'"; if(mysqli_query($con,$sql_2)) { echo '修改成功!'; echo ','; } else { echo '請再嘗試一次!'; } } } if($password != '') { require_once('dbConnect.php'); $sql_3 = "SELECT * FROM userinfo WHERE name='$name'"; $check = mysqli_fetch_array(mysqli_query($con,$sql_3)); if(isset($check)) { $sql_4 = "UPDATE userinfo SET password = '$password' WHERE name = '$name'"; if(mysqli_query($con,$sql_4)) { echo '修改成功!'; echo ','; } else { echo '請再嘗試一次!'; } } } if($email != '') { require_once('dbConnect.php'); $sql_5 = "SELECT * FROM userinfo WHERE name='$name'"; $check = mysqli_fetch_array(mysqli_query($con,$sql_5)); if(isset($check)) { $sql_6 = "UPDATE userinfo SET email = '$email' WHERE name = '$name'"; if(mysqli_query($con,$sql_6)) { echo '修改成功!'; echo ','; } else { echo '請再嘗試一次!'; } } } if($tel != '') { require_once('dbConnect.php'); $sql_7 = "SELECT * FROM userinfo WHERE name='$name'"; $check = mysqli_fetch_array(mysqli_query($con,$sql_7)); if(isset($check)) { $sql_8 = "UPDATE userinfo SET tel = '$tel' WHERE name = '$name'"; if(mysqli_query($con,$sql_8)) { echo '修改成功!'; echo ','; } else { echo '請再嘗試一次!'; } } } mysqli_close($con); } else { echo 'Error'; } ?>
留言
張貼留言