會員資料變更的處理 (教學示範)

建立 html 的顯示資料的表
<section>
<form>
<fieldset><legend>個人資料</legend>
<ol>
<li><label for="nick">暱稱:</label><input id="nick" name="nick" type="text" maxlength="30"></li>
<li><label for="district">居住區:</label><select id="district" name="district"><option value="0">縣市</option></select></li>
<li><label for="affect">感情:</label><select id="affect" name="affect"><option value="0">狀況</option></select></li>
<li><button type="submit">變更資料</button></li>
</ol>
</fieldset>
</form>
</section>
建立 SQL 資料表

①district_tw, ②status_affect, ③ac_basic

創建 PDO 物件連接資料庫
<?php
require_once 'connections/cn-prime.php';
?>
檢驗會員是否已登入並取得資料
<?php
require_once 'connections/cn-prime.php';
session_start();
if(!empty($_SESSION['logAccount'])){
 $tb_ac='ac_basic';
 $tb_dt='district_tw';
 $tb_af='status_affect';
 $sql1="SELECT email, password, nick, district, affect FROM $tb_ac WHERE id=$_SESSION[logid]";
 $stmt1=$prime->prepare($sql1);
 $stmt1->execute() or exit;
 $row=$stmt1->fetch(PDO::FETCH_OBJ);
}
else{
 header("Location:http://localhost/login.php");
}
?>
<section>
<ul><li><a href="login.php">主頁</a></li></ul>
<p>帳號:<?php echo $row->email; ?></p>
<form>
<fieldset><legend>個人資料</legend>
<ol>
<li><label for="nick">暱稱:</label><input id="nick" name="nick" type="text" maxlength="30"<?php if(!empty($row->nick)) echo ' value="'.$row->nick.'"'; ?>></li>
<li><label for="district">居住區:</label><select id="district" name="district"><option value="0">縣市</option><?php
$stmt2=$prime->prepare("SELECT * FROM $tb_dt ORDER BY id");
$stmt2->execute();
if(!empty($row->district)){
 while($row2=$stmt2->fetch(PDO::FETCH_LAZY)){
  printf('<option value="%s"%s>%s</option>',$row2->id,$row2->id==$row->district?' selected':'',$row2->district);
 }
}
else{
 while($row2=$stmt2->fetch(PDO::FETCH_LAZY)){
  printf('<option value="%s">%s</option>',$row2->id,$row2->district);
 }
} ?></select></li>
<li><label for="affect">感情:</label><select id="affect" name="affect"><option value="0">狀況</option><?php
$stmt3=$prime->prepare("SELECT * FROM $tb_af ORDER BY id");
$stmt3->execute();
if(!empty($row->affect)){
 while($row3=$stmt3->fetch(PDO::FETCH_LAZY)){
  printf('<option value="%s"%s>%s</option>',$row3->id,$row3->id==$row->affect?' selected':'',$row3->affect);
 }
}
else{
 while($row3=$stmt3->fetch(PDO::FETCH_LAZY)){
  printf('<option value="%s">%s</option>',$row3->id,$row3->affect);
 }
} ?></select></li>
<li><button type="submit">變更資料</button></li>
</ol>
</fieldset>
</form>
</section>
處理變更的資料
<?php
require_once 'connections/cn-prime.php';
session_start();
if(!empty($_SESSION['logAccount'])){
 $tb_ac='ac_basic';
 $tb_dt='district_tw';
 $tb_af='status_affect';
 $sql1="SELECT email, password, nick, district, affect FROM $tb_ac WHERE id=$_SESSION[logid]";
 $stmt1=$prime->prepare($sql1);
 $stmt1->execute() or exit;
 $row=$stmt1->fetch(PDO::FETCH_OBJ);
 if(filter_has_var(INPUT_POST,'updating')){
  $sql2="UPDATE $tb_ac SET nick=:nick, district=:district, affect=:affect WHERE id=$_SESSION[logid]";
  $stmt4=$prime->prepare($sql2);
  $_POST['nick'] or $_POST['nick']=NULL;
  $_POST['district'] or $_POST['district']=NULL;
  $_POST['affect'] or $_POST['affect']=NULL;
  $stmt4->bindParam(':nick',$_POST['nick']);
  $stmt4->bindParam(':district',$_POST['district'],PDO::PARAM_INT);
  $stmt4->bindParam(':affect',$_POST['affect'],PDO::PARAM_INT);
  if($stmt4->execute()){
   $isRevised=true;
  }
 }
}
else{
 header("Location:http://localhost/login.php");
}
?>
<section>
<ul><li><a href="login.php">主頁</a></li></ul>
<p>帳號:<?php echo $row->email; ?></p>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<fieldset><legend>個人資料</legend>
<ol>
<li><label for="nick">暱稱:</label><input id="nick" name="nick" type="text" maxlength="30"<?php if(!empty($row->nick)) echo ' value="'.$row->nick.'"'; ?>></li>
<li><label for="district">居住區:</label><select id="district" name="district"><option value="0">縣市</option><?php
$stmt2=$prime->prepare("SELECT * FROM $tb_dt ORDER BY id");
$stmt2->execute();
if(!empty($row->district)){
 while($row2=$stmt2->fetch(PDO::FETCH_LAZY)){
  printf('<option value="%s"%s>%s</option>',$row2->id,$row2->id==$row->district?' selected':'',$row2->district);
 }
}
else{
 while($row2=$stmt2->fetch(PDO::FETCH_LAZY)){
  printf('<option value="%s">%s</option>',$row2->id,$row2->district);
 }
} ?></select></li>
<li><label for="affect">感情:</label><select id="affect" name="affect"><option value="0">狀況</option><?php
$stmt3=$prime->prepare("SELECT * FROM $tb_af ORDER BY id");
$stmt3->execute();
if(!empty($row->affect)){
 while($row3=$stmt3->fetch(PDO::FETCH_LAZY)){
  printf('<option value="%s"%s>%s</option>',$row3->id,$row3->id==$row->affect?' selected':'',$row3->affect);
 }
}
else{
 while($row3=$stmt3->fetch(PDO::FETCH_LAZY)){
  printf('<option value="%s">%s</option>',$row3->id,$row3->affect);
 }
} ?></select></li>
<li><button type="submit" name="updating" value="1">變更資料</button></li>
</ol>
</fieldset>
</form>
</section>
檢驗輸入資料是否確實有變更
<?php
require_once 'connections/cn-prime.php';
session_start();
if(!empty($_SESSION['logAccount'])){
 $tb_ac='ac_basic';
 $tb_dt='district_tw';
 $tb_af='status_affect';
 $sql1="SELECT email, password, nick, district, affect FROM $tb_ac WHERE id=$_SESSION[logid]";
 $stmt1=$prime->prepare($sql1);
 $stmt1->execute() or exit;
 $row=$stmt1->fetch(PDO::FETCH_OBJ);
 if(filter_has_var(INPUT_POST,'updating')){
  $sql2="UPDATE $tb_ac SET nick=:nick, district=:district, affect=:affect WHERE id=$_SESSION[logid]";
  $stmt4=$prime->prepare($sql2);
  $_POST['nick'] or $_POST['nick']=NULL;
  $_POST['district'] or $_POST['district']=NULL;
  $_POST['affect'] or $_POST['affect']=NULL;
  if($_POST['nick']!=$row->nick || $_POST['district']!=$row->district || $_POST['affect']!=$row->affect){
   $stmt4->bindParam(':nick',$_POST['nick']);
   $stmt4->bindParam(':district',$_POST['district'],PDO::PARAM_INT);
   $stmt4->bindParam(':affect',$_POST['affect'],PDO::PARAM_INT);
   if($stmt4->execute()){
    if($stmt1->execute()){
     $row=$stmt1->fetch(PDO::FETCH_OBJ);
     $_SESSION['logNickname']=$row->nick?$row->nick:'隱名埋姓';
    }
   }
  }
 }
}
else{
 header("Location:http://localhost/login.php");
}
?>
建立 html 的變更密碼的表
<section>
<form>
<fieldset><legend>密碼變更</legend>
<ol>
<li><label for="pwd1">新密碼:</label><input id="pwd1" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><label for="pwd2">再輸入一次新密碼:</label><input id="pwd2" name="password2" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><button type="submit">變更密碼</button><em>變更後須重新登入</em></li>
</ol>
</fieldset>
</form>
</section>

善用 html 5 填表元素新屬性

<section>
<form>
<fieldset><legend>密碼變更</legend>
<ol>
<li><label for="pwd1">新密碼:</label><input id="pwd1" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合" title="新密碼:大小寫英文字母及數字混合,6~18字,如 A12Rd6" minlength="6" required pattern="(?=^[A-Za-z0-9]{6,18}$)((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]))^.*$"></li>
<li><label for="pwd2">再輸入一次新密碼:</label><input id="pwd2" name="password2" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合" title="再輸入一次新密碼" minlength="6" required pattern="(?=^[A-Za-z0-9]{6,18}$)((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]))^.*$"></li>
<li><button type="submit">變更密碼</button><em>變更後須重新登入</em></li>
</ol>
</fieldset>
</form>
</section>
處理變更密碼
<?php
/**
 * v.0.2.0 latest:2014/11/16
 * ©webchain(不惑仔), all rights reserved. Free for commercial and personal use.
 */ 
class validux{
public static function isAlphanumMixed($subject,$minLength=3,$maxLength=''){
 $p='/(?=^[A-Za-z0-9]{'.$minLength.','.$maxLength.'}$)((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]))^.*$/';
 return (bool)preg_match($p,$subject);
}
}
?>
<?php
function autoloadClass($class){require_once "classes/$class.php";}spl_autoload_register('autoloadClass');
require_once 'connections/cn-prime.php';
session_start();
if(!empty($_SESSION['logAccount'])){
 $tb_ac='ac_basic';
 $tb_dt='district_tw';
 $tb_af='status_affect';
 $sql1="SELECT email, password, nick, district, affect FROM $tb_ac WHERE id=$_SESSION[logid]";
 $stmt1=$prime->prepare($sql1);
 $stmt1->execute() or exit;
 $row=$stmt1->fetch(PDO::FETCH_OBJ);
 if(filter_has_var(INPUT_POST,'updating')){
  $sql2="UPDATE $tb_ac SET nick=:nick, district=:district, affect=:affect WHERE id=$_SESSION[logid]";
  $stmt4=$prime->prepare($sql2);
  $_POST['nick'] or $_POST['nick']=NULL;
  $_POST['district'] or $_POST['district']=NULL;
  $_POST['affect'] or $_POST['affect']=NULL;
  if($_POST['nick']!=$row->nick || $_POST['district']!=$row->district || $_POST['affect']!=$row->affect){
   $stmt4->bindParam(':nick',$_POST['nick']);
   $stmt4->bindParam(':district',$_POST['district'],PDO::PARAM_INT);
   $stmt4->bindParam(':affect',$_POST['affect'],PDO::PARAM_INT);
   if($stmt4->execute()){
    if($stmt1->execute()){
     $row=$stmt1->fetch(PDO::FETCH_OBJ);
     $_SESSION['logNickname']=$row->nick?$row->nick:'隱名埋姓';
    }
   }
  }
 }
 if(filter_has_var(INPUT_POST,'pwdrenewing')){
  if(validux::isAlphanumMixed($_POST['password'],6,18)){
   if($_POST['password']==$_POST['password2']){
    if($_POST['password']!=$row->password){
     $stmt5=$prime->prepare("UPDATE $tb_ac SET password=:password WHERE id=$_SESSION[logid]");
     $stmt5->bindParam(':password',$_POST['password']);
     if($stmt5->execute()){
      $newPwd=true;
     }
    }
   }
   else{
    $failedPwd=true;
   }
  }
  else{
   $invalidPwd=true;
  }
 }
}
else{
 header("Location:http://localhost/login.php");
}
?>
<section>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<fieldset><legend>密碼變更</legend>
<ol>
<li><label for="pwd1">新密碼:</label><input id="pwd1" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><label for="pwd2">再輸入一次新密碼:</label><input id="pwd2" name="password2" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"></li>
<li><button type="submit" name="pwdrenewing" value="1">變更密碼</button><em>變更後須重新登入</em></li>
</ol>
</fieldset>
</form>
</section>
密碼輸入錯誤處理
<section>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<fieldset><legend>密碼變更</legend>
<ol>
<li><label for="pwd1">新密碼:</label><input id="pwd1" name="password" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"><?php if(!empty($invalidPwd)) echo '<label for="pwd1">須填入有效密碼。</label>'; ?></li>
<li><label for="pwd2">再輸入一次新密碼:</label><input id="pwd2" name="password2" type="password" maxlength="18" placeholder="6-18大小寫字母數字混合"><?php if(!empty($failedPwd)) echo '<label for="pwd1">不相符,請重新輸入。</label>'; ?></li>
<li><button type="submit" name="pwdrenewing" value="1">變更密碼</button><em>變更後須重新登入</em></li>
</ol>
</fieldset>
</form>
</section>
密碼變更成功後重新登入處理
<?php
/**
 * v.0.1.0 latest:2013/2/10
 * ©webchain(不惑仔), all rights reserved. Free for commercial and personal use.
 */
class xsession{
public static function discard(){
 $_SESSION=[];
 if(ini_get("session.use_cookies")){
  $params=session_get_cookie_params();
  setcookie(session_name(),'',time()-3600,$params["path"],$params["domain"],$params["secure"],$params["httponly"]);
 }
 session_destroy();
}
}
?>
<?php
function autoloadClass($class){require_once "classes/$class.php";}spl_autoload_register('autoloadClass');
require_once 'connections/cn-prime.php';
session_start();
if(!empty($_SESSION['logAccount'])){
 $tb_ac='ac_basic';
 $tb_dt='district_tw';
 $tb_af='status_affect';
 $sql1="SELECT email, password, nick, district, affect FROM $tb_ac WHERE id=$_SESSION[logid]";
 $stmt1=$prime->prepare($sql1);
 $stmt1->execute() or exit;
 $row=$stmt1->fetch(PDO::FETCH_OBJ);
 if(filter_has_var(INPUT_POST,'updating')){
  $sql2="UPDATE $tb_ac SET nick=:nick, district=:district, affect=:affect WHERE id=$_SESSION[logid]";
  $stmt4=$prime->prepare($sql2);
  $_POST['nick'] or $_POST['nick']=NULL;
  $_POST['district'] or $_POST['district']=NULL;
  $_POST['affect'] or $_POST['affect']=NULL;
  if($_POST['nick']!=$row->nick || $_POST['district']!=$row->district || $_POST['affect']!=$row->affect){
   $stmt4->bindParam(':nick',$_POST['nick']);
   $stmt4->bindParam(':district',$_POST['district'],PDO::PARAM_INT);
   $stmt4->bindParam(':affect',$_POST['affect'],PDO::PARAM_INT);
   if($stmt4->execute()){
    if($stmt1->execute()){
     $row=$stmt1->fetch(PDO::FETCH_OBJ);
     $_SESSION['logNickname']=$row->nick?$row->nick:'隱名埋姓';
    }
   }
  }
 }
 if(filter_has_var(INPUT_POST,'pwdrenewing')){
  if(validux::isAlphanumMixed($_POST['password'],6,18)){
   if($_POST['password']==$_POST['password2']){
    if($_POST['password']!=$row->password){
     $stmt5=$prime->prepare("UPDATE $tb_ac SET password=:password WHERE id=$_SESSION[logid]");
     $stmt5->bindParam(':password',$_POST['password']);
     if($stmt5->execute()){
      xsession::discard();
      header("Location:http://localhost/login.php");
     }
    }
   }
   else{
    $failedPwd=true;
   }
  }
  else{
   $invalidPwd=true;
  }
 }
}
else{
 header("Location:http://localhost/login.php");
}
?>
建立 html 的刪除帳號的鈕
<section>
<form>
<p><button type="submit">刪除帳號</button></p>
</form>
</section>
處理刪除帳號並登出
<?php
function autoloadClass($class){require_once "classes/$class.php";}spl_autoload_register('autoloadClass');
require_once 'connections/cn-prime.php';
session_start();
if(!empty($_SESSION['logAccount'])){
 $tb_ac='ac_basic';
 $tb_dt='district_tw';
 $tb_af='status_affect';
 if(filter_has_var(INPUT_POST,'deleting')){
  $stmt6=$prime->prepare("DELETE FROM $tb_ac WHERE id=$_SESSION[logid]");
  if($stmt6->execute()){
   xsession::discard();
   header("Location:http://localhost/login.php");
  }
 }
 $sql1="SELECT email, password, nick, district, affect FROM $tb_ac WHERE id=$_SESSION[logid]";
 $stmt1=$prime->prepare($sql1);
 $stmt1->execute() or exit;
 $row=$stmt1->fetch(PDO::FETCH_OBJ);
 if(filter_has_var(INPUT_POST,'updating')){
  $sql2="UPDATE $tb_ac SET nick=:nick, district=:district, affect=:affect WHERE id=$_SESSION[logid]";
  $stmt4=$prime->prepare($sql2);
  $_POST['nick'] or $_POST['nick']=NULL;
  $_POST['district'] or $_POST['district']=NULL;
  $_POST['affect'] or $_POST['affect']=NULL;
  if($_POST['nick']!=$row->nick || $_POST['district']!=$row->district || $_POST['affect']!=$row->affect){
   $stmt4->bindParam(':nick',$_POST['nick']);
   $stmt4->bindParam(':district',$_POST['district'],PDO::PARAM_INT);
   $stmt4->bindParam(':affect',$_POST['affect'],PDO::PARAM_INT);
   if($stmt4->execute()){
    if($stmt1->execute()){
     $row=$stmt1->fetch(PDO::FETCH_OBJ);
     $_SESSION['logNickname']=$row->nick?$row->nick:'隱名埋姓';
    }
   }
  }
 }
 if(filter_has_var(INPUT_POST,'pwdrenewing')){
  if(validux::isAlphanumMixed($_POST['password'],6,18)){
   if($_POST['password']==$_POST['password2']){
    if($_POST['password']!=$row->password){
     $stmt5=$prime->prepare("UPDATE $tb_ac SET password=:password WHERE id=$_SESSION[logid]");
     $stmt5->bindParam(':password',$_POST['password']);
     if($stmt5->execute()){
      xsession::discard();
      header("Location:http://localhost/login.php");
     }
    }
   }
   else{
    $failedPwd=true;
   }
  }
  else{
   $invalidPwd=true;
  }
 }
}
else{
 header("Location:http://localhost/login.php");
}
?>
<section>
<form method="post" action="<?php echo basename(__FILE__); ?>">
<p><button type="submit" name="deleting" value="1">刪除帳號</button></p>
</form>
</section>
參考資源

更新日期:

google 論壇

App javascript (groups.google.com/group/app-javascript/)