2016/04/18 ①注册 注册处理 ② 审核 审核处理 ③登录 登录处理 ④需要jquery-1.11.2.min.js DBDA.php
① 注册 zhuceye.php
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <script src="jquery-1.11.2.min.js"></script> 7 </head> 8 <body> 9 <h1>注册页面</h1> 10 <div> 11 <div>用户名:<input type="text" id="uid"></input></div> 12 <div>密码:<input type="text" id="pwd"></input></div> 13 <div>姓名:<input type="text" id="name"></input></div> 14 <div>性别:<input type="radio" name="sex" value="true" id="nan" checked="checked">男 <input type="radio" name="sex" value="false" />女</div> 15 <div>生日:<input type="text" id="birthday"></input></div> 16 <div>工号:<input type="text" id="code"></input></div> 17 <div><input id="btn" type="button" value="注册"></input></div> 18 19 </div> 20 </body> 21 </html> 22 <script type="text/javascript"> 23 $(document).ready(function(e){ 24 $("#btn").click(function(){ 25 var uid=$("#uid").val(); 26 var pwd=$("#pwd").val(); 27 var name=$("#name").val(); 28 var sex=$("#nan")[0].checked; 29 var birthday=$("#birthday").val(); 30 var code=$("#code").val(); 31 32 $.ajax({ 33 url:"zhucechuli.php", 34 data:{uid:uid,pwd:pwd,name:name,sex:sex,birthday:birthday,code:code}, 35 type:"POST", 36 dataType:"TEXT", 37 success:function(data){ 38 39 if (data=="OK") { 40 alert("注册成功!"); 41 42 } 43 else{ 44 alert("注册失败!"); 45 } 46 } 47 48 49 }); 50 }) 51 }); 52 </script>
注册处理 zhucechuli.php
1 <?php 2 include("DBDA.php"); 3 $uid=$_POST["uid"]; 4 $pwd=$_POST["pwd"]; 5 $name=$_POST["name"]; 6 $sex=$_POST["sex"]; 7 $birthday=$_POST["birthday"]; 8 $code=$_POST["code"]; 9 10 $db=new DBDA(); 11 12 $sql="insert into Shenhe values('".$uid."','".$pwd."','".$name."',".$sex.",'".$birthday."','".$code."',false)"; 13 $str=$db->StrQuery($sql,0,"mydb"); 14 15 echo $str; 16 ?>
显示效果:
数据库中:
②审核 main.php
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <h1>注册审核页面</h1> 9 <table width="100%" border="1" cellpadding="0" cellspacing="0"> 10 <tr> 11 <td>用户编号</td> 12 <td>姓名</td> 13 <td>性别</td> 14 <td>生日</td> 15 <td>工号</td> 16 <td>状态</td> 17 </tr> 18 <?php 19 include("DBDA.php"); 20 $db=new DBDA(); 21 $sql="select * from Shenhe"; 22 $str=$db->StrQuery($sql,1,"mydb"); 23 24 $hang=explode("|",$str); 25 for ($i=0; $i <count($hang) ; $i++) { 26 $lie=explode("^",$hang[$i]); 27 28 $sexName=$lie[3]?"男":"女"; 29 $zhuangtai=$lie[6]?"<span style='width:80px;height:28px;background-color:green;color:white'>已通过</span>":"<a href='shenli.php?name={$lie[2]}'>审核</a>"; 30 31 echo "<tr><td>{$lie[0]}</td><td>{$lie[2]}</td><td>{$sexName}</td><td>{$lie[4]}</td><td>{$lie[5]}</td><td>{$zhuangtai}</td></tr>"; 32 } 33 34 ?> 35 </table> 36 </body> 37 </html>
点击审核:
审核处理 shenli.php
1 <?php 2 $name=$_GET["name"]; 3 4 include("DBDA.php"); 5 6 $db=new DBDA(); 7 8 $sql="update Shenhe set IsOK=true where name='".$name."'"; 9 10 $str=$db->StrQuery($sql,0,"mydb"); 11 12 13 header("Location:main.php") 14 15 ?>
③登录 denglu.php
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>denglu</title> 6 </head> 7 <body> 8 <form action="dengluchuli.php" method="post"> 9 <h1>登录页面</h1> 10 <div>用户名:<input type="text" name="name"></input></div> 11 <div>密码:<input type="text" name="pwd"></input></div> 12 <div><input type="submit" value="登录"></input></div> 13 </form> 14 </body> 15 </html>
正常登录显示:
非正常登录:跳转回登录页
登录处理 dengluchuli.php
1 <?php 2 include("DBDA.php"); 3 $name=$_POST["name"]; 4 $pwd=$_POST["pwd"]; 5 6 $db=new DBDA(); 7 8 $sql="select count(*) from Shenhe where Name='".$name."' and Pwd='".$pwd."' and IsOK=true"; 9 10 $str=$db->StrQuery($sql,1,"mydb"); 11 12 13 if ($str==1) { 14 header("Location:main.php"); 15 } 16 else{ 17 header("Location:denglu.php"); 18 } 19 ?>
④需要引入的有 jquery-1.11.2.min.js 之前整理页上都有 可看前面
DBDA.php
1 <?php 2 3 class DBDA 4 { 5 public $host = "localhost"; //服务器地址 6 public $uid = "root"; //数据库的用户名 7 public $pwd = "123"; //数据库的密码 8 9 //执行SQL语句,返回相应结果的函数 10 //$sql是要执行的SQL语句 11 //$type是SQL语句的类型,0代表增删改,1代表查询 12 //$db代表要操作的数据库 13 public function Query($sql,$type,$db) 14 { 15 //造连接对象 16 $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); 17 18 //判断连接是否成功 19 !mysqli_connect_error() or die("连接失败!"); 20 21 //执行SQL语句 22 $result = $conn->query($sql); 23 24 //判断SQL语句类型 25 if($type==1) 26 { 27 //如果是查询语句返回结果集的二维数组 28 return $result->fetch_all(); 29 } 30 else 31 { 32 //如果是其他语句,返回true或false 33 return $result; 34 } 35 } 36 37 //Ajax调用返回JSON 38 public function JsonQuery($sql,$type=1,$db="test2") 39 { 40 //定义数据源 41 $dsn = "mysql:dbname={$db};host={$this->host}"; 42 //造pdo对象 43 $pdo = new PDO($dsn,"{$this->uid}","{$this->pwd}"); 44 45 46 //准备执行SQL语句 47 $st = $pdo->prepare($sql); 48 49 //执行预处理语句 50 if($st->execute()) 51 { 52 if($type==1) 53 { 54 $attr = $st->fetchAll(PDO::FETCH_ASSOC); 55 return json_encode($attr); 56 } 57 else 58 { 59 if($st) 60 { 61 return "OK"; 62 } 63 else 64 { 65 return "NO"; 66 } 67 } 68 69 } 70 else 71 { 72 echo "执行失败!"; 73 } 74 75 76 77 } 78 //Ajax调用返回字符串 79 public function StrQuery($sql,$type,$db) 80 { 81 //造连接对象 82 $conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); 83 84 //判断连接是否成功 85 !mysqli_connect_error() or die("连接失败!"); 86 87 //执行SQL语句 88 $result = $conn->query($sql); 89 90 //判断SQL语句类型 91 if($type==1) 92 { 93 $attr = $result->fetch_all(); 94 $str = ""; 95 //如果是查询语句返回字符串 96 for($i=0;$i<count($attr);$i++) 97 { 98 for($j=0;$j<count($attr[$i]);$j++) 99 { 100 $str = $str.$attr[$i][$j]; 101 $str = $str."^"; 102 } 103 $str = substr($str,0,strlen($str)-1); 104 $str = $str."|"; 105 } 106 $str = substr($str,0,strlen($str)-1); 107 108 return $str; 109 } 110 else 111 { 112 //如果是其他语句,返回true或false 113 if($result) 114 { 115 return "OK"; 116 } 117 else 118 { 119 return "NO"; 120 } 121 } 122 } 123 124 125 }