一、效果




二、代碼框架

functions文件夾里面是封裝的mysqli的數據庫操作函數和一個跳轉的函數
student文件夾里面就是學生管理系統的主界面
application下的index.php就是登陸界面
application下的doAction.php頁面就是對application下的index.php的各種響應
data文件夾里面是mysql生產數據庫和表的代碼
三、功能實現
1、郵箱驗證功能的實現
是通過調用第三方工具swiftmailer-master來實現的,就是簡單的調用這個第三方的工具就可以實現了
先弄一個smtp服務器(這里是用的sina,發送郵件的賬號名和密碼是clivelyn@sina.com和lin123)來發送郵件,發送給用戶注冊的那個郵箱
當然發送郵件你肯定要確定發件人,發送主題,發送的郵件的內容,發送的郵件的內容里面會有一個激活鏈接,當然這個鏈接是要加密的
賬號激活與否是通過數據庫中的status關鍵字來確定的,status為0表示沒激活,為1表示激活了
核心代碼如下:
1 //發送激活郵件
2 //初始化郵件服務器對象
3 $transport=Swift_SmtpTransport::newInstance('smtp.sina.com',25);
4 //設置用戶名和密碼
5 $transport->setUsername('clivelyn@sina.com');
6 $transport->setPassword('lin123');
7 $mailer=Swift_Mailer::newInstance($transport);//發送郵件對象
8 $message=Swift_Message::newInstance();//郵件信息對象
9 $message->setFrom(array('clivelyn@sina.com'));//誰發送的
10 $message->setTo($email);//發送給誰
11 $message->setSubject('注冊賬號激活郵件');//設置郵件主題
12
13 $activeStr="?act=active&username={$username}&token={$token}";
14 $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].
15 $activeStr;
16 // echo $url;
17 // echo $url.urlencode($activeStr);
18 $urlEncode=urlencode($url);
19 //http://localhost/test/PHPAdvance/MySQLi/application/doAction.php?act=active&username=king&token=74bccca6db02607e7dd75f088ee6fee8
20 $emailBody=<<<EOF
21 歡迎{$username}使用賬號激活功能
22 請點擊鏈接激活賬號:
23 <a href='{$url}' target='_blank'>{$urlEncode}</a> <br />
24 (該鏈接在24小時內有效)
25 如果上面不是鏈接形式,請將地址復制到您的瀏覽器(例如IE)的地址欄再訪問。
26 EOF;
27 $message->setBody($emailBody,"text/html",'utf-8');
28 try{
29 $res1=$mailer->send($message);
30 var_dump($res);
31 if($res && $res1){
32 mysqli_commit($link);
33 mysqli_autocommit($link, TRUE);
34 alertMes('注冊成功,立即激活使用', 'index.php');
35 }else{
36 mysqli_rollback($link);
37 alertMes('注冊失敗,重新注冊','index.php');
38 }
39
40 }catch(Swift_ConnectionException $e){
41 echo '123';
42 die('郵件服務器錯誤:').$e->getMessage();
43 }
44 break;
18、這里注意18行加密的寫法,urlencode
https://www.yaoruanwen.com/n/480598.html
https://www.yaoruanwen.com/n/460064.html
https://www.yaoruanwen.com/n/457889.html
http://www.zhongjinnews.com/v-1-1288142.aspx
http://www.zhongjinnews.com/v-1-1286052.aspx
https://sx.wang1314.com/topic/2020/11/14/55345/273611-1.html
2、mysqli的用法
在代碼用mysqli全部被再次封裝成了函數,所以非常方便使用,包括連接數據庫,增刪改查,mysqli的操作數據庫也比較方便
還有一個配置文件config.php用來存放用到的數據庫的連接參數
核心代碼:
1 <?php
2 /**
3 * 連接
4 * @param string $host
5 * @param string $user
6 * @param string $password
7 * @param string $charset
8 * @param string $database
9 * @return object 連接標識符
10 */
11 function connect1($host,$user,$password,$charset,$database) {
12 $link = mysqli_connect ( $host, $user, $password ) or die ( '數據庫連接失敗<br/>ERROR ' . mysqli_connect_errno () . ':' . mysqli_connect_error () );
13 mysqli_set_charset ( $link, $charset );
14 mysqli_select_db ( $link, $database ) or die ( '指定數據庫打開失敗<br/>ERROR ' . mysqli_errno ( $link ) . ':' . mysqli_error ( $link ) );
15 return $link;
16 }
17 /**
18 * 連接 需要傳遞數組
19 * @param array $config
20 * @return object
21 */
22 function connect2($config) {
23 $link = mysqli_connect ( $config ['host'], $config ['user'], $config ['password'] ) or die ( '數據庫連接失敗<br/>ERROR ' . mysqli_connect_errno () . ':' . mysqli_connect_error () );
24 mysqli_set_charset ( $link, $config ['charset'] );
25 mysqli_select_db ( $link, $config ['dbName'] ) or die ( '指定數據庫打開失敗<br/>ERROR ' . mysqli_errno ( $link ) . ':' . mysqli_error ( $link ) );
26 return $link;
27 }
28 /**
29 * 用常量的形式建立連接
30 * @return unknown
31 */
32 function connect3(){
33 $link = mysqli_connect ( DB_HOST, DB_USER, DB_PWD ) or die ( '數據庫連接失敗<br/>ERROR ' . mysqli_connect_errno () . ':' . mysqli_connect_error () );
34 mysqli_set_charset ( $link, DB_CHARSET );
35 mysqli_select_db ( $link, DB_DBNAME ) or die ( '指定數據庫打開失敗<br/>ERROR ' . mysqli_errno ( $link ) . ':' . mysqli_error ( $link ) );
36 return $link;
37 }
38
39 /*
40 array(
41 'username'=>'king',
42 'password'=>'king',
43 'age'=>'12',
44 'regTime'=>'123123123'
45 );
46 INSERT user(username,password,age,regTime) VALUES('king','king','12','123123123');
47 */
48 /**
49 * 插入操作
50 * @param object $link
51 * @param array $data
52 * @param string $table
53 * @return boolean
54 */
55 function insert($link,$data,$table){
56 $keys = join ( ',', array_keys ( $data ) );
57 $vals = "'" . join ( "','", array_values ( $data ) ) . "'";
58 $query = "INSERT {$table}({$keys}) VALUES({$vals})";
59 $res = mysqli_query ( $link, $query );
60 if ($res) {
61 return mysqli_insert_id ( $link );
62 } else {
63 return false;
64 }
65 }
66
67 /*
68 array(
69 'username'=>'king123',
70 'password'=>'king123',
71 'age'=>'32',
72 'regTime'=>'123123123'
73 );
74 UPDATE user SET username='king123',password='king123',age='32',regTime='123123123' WHERE id=1
75 */
76 /**
77 * 更新操作
78 * @param object $link
79 * @param array $data
80 * @param string $table
81 * @param string $where
82 * @return boolean
83 */
84 function update($link, $data, $table, $where = null) {
85 foreach ( $data as $key => $val ) {
86 $set .= "{$key}='{$val}',";
87 }
88 $set = trim ( $set, ',' );
89 $where = $where == null ? '' : ' WHERE ' . $where;
90 $query = "UPDATE {$table} SET {$set} {$where}";
91 $res = mysqli_query ( $link, $query );
92 if ($res) {
93 return mysqli_affected_rows ( $link );
94 } else {
95 return false;
96 }
97 }
98
99 //DELETE FROM user WHERE id=
100 /**
101 * 刪除操作
102 * @param object $link
103 * @param string $table
104 * @param string $where
105 * @return boolean
106 */
107 function delete($link, $table, $where = null) {
108 $where = $where ? ' WHERE ' . $where : '';
109 $query = "DELETE FROM {$table} {$where}";
110 $res = mysqli_query ( $link, $query );
111 if ($res) {
112 return mysqli_affected_rows ( $link );
113 } else {
114 return false;
115 }
116 }
117
118 /**
119 * 查詢指定記錄
120 * @param object $link
121 * @param string $query
122 * @param string $result_type
123 * @return array|boolean
124 */
125 function fetchOne($link, $query, $result_type = MYSQLI_ASSOC) {
126 $result = mysqli_query ( $link, $query );
127 if ($result && mysqli_num_rows ( $result ) > 0) {
128 $row = mysqli_fetch_array ( $result, $result_type );
129 return $row;
130 } else {
131 return false;
132 }
133 }
134
135 /**
136 * 查詢所有記錄
137 * @param object $link
138 * @param string $query
139 * @param string $result_type
140 * @return array|boolean
141 */
142 function fetchAll($link, $query, $result_type = MYSQLI_ASSOC) {
143 $result = mysqli_query ( $link, $query );
144 if ($result && mysqli_num_rows ( $result ) > 0) {
145 while ( $row = mysqli_fetch_array ( $result, $result_type ) ) {
146 $rows [] = $row;
147 }
148 return $rows;
149 } else {
150 return false;
151 }
152 }
153
154 /**
155 * 得到表中的記錄數
156 * @param object $link
157 * @param string $table
158 * @return number|boolean
159 */
160 function getTotalRows($link, $table) {
161 $query = "SELECT COUNT(*) AS totalRows FROM {$table}";
162 $result = mysqli_query ( $link, $query );
163 if ($result && mysqli_num_rows ( $result ) == 1) {
164 $row = mysqli_fetch_assoc ( $result );
165 return $row ['totalRows'];
166 } else {
167 return false;
168 }
169 }
170
171 /**
172 * 得到結果集的記錄條數
173 * @param object $link
174 * @param string $query
175 * @return boolean
176 */
177 function getResultRows($link, $query) {
178 $result = mysqli_query ( $link, $query );
179 if ($result) {
180 return mysqli_num_rows ( $result );
181 } else {
182 return false;
183 }
184 }
185
186
187
188 /**
189 * @param object $link
190 */
191 function getServerInfo($link) {
192 return mysqli_get_server_info ( $link );
193 }
194 /**
195 * @param object $link
196 */
197 function getClientInfo($link) {
198 return mysqli_get_client_info ( $link );
199 }
200
201 /**
202 * @param object $link
203 */
204 function getHostInfo($link){
205 return mysqli_get_host_info($link);
206 }
207
208 /**
209 * @param object $link
210 */
211 function getProtoInfo($link) {
212 return mysqli_get_proto_info ( $link );
213 }
配置文件config.php
1 <?php
2 $config = array(
3 'host'=>'localhost',
4 'user'=>'root',
5 'password'=>'root',
6 'charset'=>'utf8',
7 'dbName'=>'51zxw'
8 );
9
10
11 define('DB_HOST','localhost');
12 define('DB_USER','root');
13 define('DB_PWD','root');
14 define('DB_CHARSET','utf8');
15 define('DB_DBNAME','51zxw');
3、數據庫里面的數據綁定到web頁面
數據庫里面的數據綁定到web頁面是先通過mysqli取出數據,然后在把數據顯示到web頁面就好
(1)、通過mysqli取出數據核心代碼:
1 $link = connect3();
2 $query = "select * from student limit {$offSet},{$pageSize}";
3 $rows = fetchAll($link, $query);
其中的connect3()是封裝好的鏈接mysql數據庫的代碼
(2)、把數據顯示到web頁面核心代碼
1 <tbody> 2 <?php foreach ($rows as $admin):?> 3 <tr> 4 <th><?php echo $admin['id']?></th> 5 <th><?php echo $admin['username']?></th> 6 <th><?php echo $admin['age']?></th> 7 <th><?php echo $admin['sex']?></th> 8 <td> 9 <span>詳情</span> 10 <span>修改</span> 11 <span class="delect" data_id="<?php echo $admin['id']?>">刪除</span> 12 </td> 13 </tr> 14 <?php endforeach;?> 15 </tbody>
其實就用一個foreach循環遍歷取到的每一條數據就可以了
4、刪除響應的完成
刪除操作是用的ajax技術,就是通過js代碼把學生的id傳給student頁面下的doAction.php響應頁面
然后在doAction.php響應界面通過mysqli來操作數據庫完成刪除的目的
(1)、通過js代碼把學生的id傳給student頁面下的doAction.php響應頁面核心代碼
1 <?php foreach ($rows as $admin):?>
2 <tr>
3 <th><?php echo $admin['id']?></th>
4 <th><?php echo $admin['username']?></th>
5 <th><?php echo $admin['age']?></th>
6 <th><?php echo $admin['sex']?></th>
7 <td>
8 <span>詳情</span>
9 <span>修改</span>
10 <span class="delect" data_id="<?php echo $admin['id']?>">刪除</span>
11 </td>
12 </tr>
13 <?php endforeach;?>
14 </tbody>
15 <script>
16 $(function(){
17 $(".delect").click(function(){
18 var id = $(this).attr("data_id");
19 var url = "doAction.php?id="+id;
20 $.get(url);
21 $(this).parent().parent().empty();
22 });
23 });
24 </script>
(2)、在doAction.php響應界面通過mysqli來操作數據庫完成刪除的目的
1 <?php
2 header("content-type:text/html;charset=utf-8");
3 require_once '../config/config.php';
4 require_once '../functions/common.func.php';
5 require_once '../functions/mysql.func.php';
6 $link = connect3();
7 $table = "student";
8 $id = isset($_GET["id"])?$_GET["id"]:"";
9 if($id==""){
10 //這里是插入學生,本來應該是判斷action為add的
11 $username = $_GET["username"];
12 $age = $_GET["age"];
13 $sex = $_GET["sex"];
14 $data = compact('username','age','sex');
15 $res = insert($link, $data, $table);
16 if($res){
17 alertMes("插入數據成功", "layout-index.php");
18 }else{
19 alertMes("插入失敗", "layout-index.php");
20 }
21 }else{
22 delete($link, $table,"id = ".$id);
23 }
就是第22行的代碼,第10到第二十行代碼時插入操作的代碼
5、增加操作的實現
增加操作就是通過在layout-form.html頁面下填好數據傳到doAction響應頁面,然后在doAction響應頁面調用mysqli來完成數據庫的插入
(1)、在layout-form.html頁面下填好數據傳到doAction響應頁面核心代碼
1 <div class="panel panel-default"> 2 <div class="panel-heading">新增學生</div> 3 <div class="panel-body"> 4 <form action="doAction.php?act=add" method="get" class="form-horizontal" role="form"> 5 <div class="form-group"> 6 <label class="col-sm-2 control-label">姓名</label> 7 <div class="col-sm-5"> 8 <input type="text" name="username" class="form-control" placeholder="姓名"> 9 </div> 10 <div class="col-sm-5"> 11 <p class="form-control-static text-danger">姓名不能為空</p> 12 </div> 13 </div>
(2)、在doAction響應頁面調用mysqli來完成數據庫的插入
1 <?php
2 header("content-type:text/html;charset=utf-8");
3 require_once '../config/config.php';
4 require_once '../functions/common.func.php';
5 require_once '../functions/mysql.func.php';
6 $link = connect3();
7 $table = "student";
8 $id = isset($_GET["id"])?$_GET["id"]:"";
9 if($id==""){
10 //這里是插入學生,本來應該是判斷action為add的
11 $username = $_GET["username"];
12 $age = $_GET["age"];
13 $sex = $_GET["sex"];
14 $data = compact('username','age','sex');
15 $res = insert($link, $data, $table);
16 if($res){
17 alertMes("插入數據成功", "layout-index.php");
18 }else{
19 alertMes("插入失敗", "layout-index.php");
20 }
21 }else{
22 delete($link, $table,"id = ".$id);
23 }
1、第10到第20行就是插入數據的代碼
2、這里是為了圖方便這樣寫,本來應該是判斷act是add的情況下,把這段代碼放在if那里面去的
6、分頁操作的實現
分頁操作是通過在數據庫中查到總條數,然后知道有多少頁,每一頁的信息查詢就是通過mysql的limit關鍵詞來實現的,然后把查到的數據放到對應的頁里面去就好了
(1)、取數據及判斷分頁的核心代碼:
1 <?php
2 require_once '../functions/mysql.func.php';
3 require_once '../config/config.php';
4 header("content-type:text/html;charset=utf-8");
5 $link = connect3();
6 $page = $_GET['page']?$_GET['page']:1;
7 $pageSize = 5;
8 $offSet = ($page-1)*5;
9 $table = "student";
10 $totalRows = getTotalRows($link, $table);
11 $sumPage = ceil($totalRows/$pageSize);
12 $query = "select * from student limit {$offSet},{$pageSize}";
13 $rows = fetchAll($link, $query);
14 ?>
(2)、選擇頁面的代碼
1 <nav>
2 <ul class="pagination pull-right">
3 <?php
4 for($i = 1;$i<=$sumPage;$i++){
5 echo "<li><a href='layout-index.php?page={$i}'>$i</a></li>";
6 }
7
8 ?>
9 </ul>
10 </nav>
7、前端框架用的bootstrap

四、完整代碼
完整代碼會放在github上面,直接下載就可以用
鏈接如下:
fry404006308/PHP_StudentManage: PHP_StudentManage
https://github.com/fry404006308/PHP_StudentManage
轉載於:https://www.cnblogs.com/Renyi-Fan/p/8553223.html
