接着上篇继续学习:php实现购物车功能(上)
7、实现一个管理界面
登录界面
由以下代码实现:
7.1 admin.php
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<?php /** * @author switch * @copyright 2015 * 主管理菜单 */ //require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once ( 'book_sc_fns.php' ); session_start(); if ((@ $_post [ 'username' ]) && (@ $_post [ 'passwd' ])) //尝试登陆 { $username = $_post [ 'username' ]; $passwd = $_post [ 'passwd' ]; if (login( $username , $passwd )) { $_session [ 'admin_user' ] = $username ; } else { do_html_header( "problem:" ); echo "<p>you could not be logged in.<br /> you must be logged in to view this page.</p>"; do_html_url( 'login.php' , 'login' ); do_html_footer(); exit ; } } do_html_header( "administration" ); if (check_admin_user()) { display_admin_menu(); } else { echo "<p>you are not authorized to enter the administration area.</p>" ; do_html_url( 'login.php' , 'login' ); } do_html_footer(); ?> |
7.2 user_auth_fns.php文件中的函数login()
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function login( $username , $password ) //登录 { $conn = db_connect(); //连接数据库 if (! $conn ) return 0; //检查用户名唯一性 $query = "select * from admin where username='" . $username ."' and password = sha1( '". $password ."' )"; $result = $conn ->query( $query ); if (! $result ) return 0; if ( $result ->num_rows > 0) return 1; else return 0; } |
7.3 user_auth_fns.php文件中的函数check_admin_user()
1
|
2
3
4
5
6
7
|
function check_admin_user() //检查是否是管理员 { if (isset( $_session [ 'admin_user' ])) return true; else return false; } |
管理主界面
由以下代码实现:
7.4 output_fns.php文件中的函数display_admin_menu()
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
function display_admin_menu() //输出管理员菜单 { ?> <br /> <a href= "index.php" >go to main site</a><br /> <a href= "insert_category_form.php" >add a new category</a><br /> <a href= "insert_book_form.php" >add a new book</a><br /> <a href= "change_password_form.php" >change admin password</a><br /> <?php } function display_button( $target , $image , $alt ) //显示按钮 { echo "<div align= \" center \"><a href=\"" . $target ."\"> <img src=\ "images/" . $image .".gif\" alt=\ "" . $alt ."\" border = \" 0 \" height = \" 50 \" width = \ " 135 \" /></a></div>" ; } |
目录添加
目录添加成功
目录页中可以看出多了novel目录
由以下代码实现:
7.5 insert_category_form.php
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php /** * @author switch * @copyright 2015 * 允许管理员向数据库中添加一个目录的表格 */ //require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含 require_once ( 'book_sc_fns.php' ); session_start(); do_html_header(); if (check_admin_user()) { display_category_form(); do_html_url( "admin.php" , "back to administrtion menu" ); } else { echo "<p>you are not authorized to enter the administation area.</p>" ; } do_html_footer(); ?> |
7.6 insert_category.php
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<?php /** * @author switch * @copyright 2015 * 向数据库中插入新目录 */ //require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含 require_once ( 'book_sc_fns.php' ); session_start(); do_html_header( "adding a category" ); if (check_admin_user()) { if (filled_out( $_post )) { $catname = $_post [ 'catname' ]; if (insert_category( $catname )) { echo "<p>category \"" . $catname . "\" was added to the database.</p>" ; } else { echo "<p>category \"" . $catname . "\" could not be added to the database.</p>" ; } } else { echo "<p>you have not filled out the form. please try again.</p>" ; } do_html_url( "admin.php" , "back to administration menu" ); } else { echo "<p>you are not authorised to view this page.</p>" ; } do_html_footer(); ?> |
管理员目录界面
目录编辑界面-可更新,删除
目录更新成功
目录主界面可以看到该目录更改成功
由以下代码实现:
7.7 edit_category_form.php
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php /** * @author switch * @copyright 2015 * 管理员编辑目录的表单 */ //require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once ( 'book_sc_fns.php' ); session_start(); do_html_header( "edit category" ); if (check_admin_user()) { if ( $catname = get_category_name( $_get [ 'catid' ])) { $catid = $_get [ 'catid' ]; $cat = compact( 'catname' , 'catid' ); display_category_form( $cat ); } else { echo "<p>could not retrieve category details.</p>" ; } do_html_url( "admin.php" , "back to administration menu" ); } else { echo "<p>you are not authorized to enter the administration area.</p>" ; } do_html_footer(); ?> |
7.8 edit_category.php
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<?php /** * @author switch * @copyright 2015 * 更新数据库中的目录 */ //require_once语句和require语句完全相同,唯一区别是php会检查该文件是否已经被包含过,如果是则不会再次包含。 require_once ( 'book_sc_fns.php' ); session_start(); do_html_header( "updating category" ); if (check_admin_user()) { if (filled_out( $_post )) { if (update_category( $_post [ 'catid' ], $_post [ 'catname' ])) { echo "<p>category was updated.</p>" ; } else { echo "<p>category could not be updated.</p>" ; } } else { echo "<p>you have not filled out the form. please try again.</p>" ; } do_html_url( "admin.php" , "back to administration menu" ); } else { echo "<p>you are not authorised to view this page.</p>" ; } do_html_footer(); ?> |
7.9 admin_fns.php
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
<?php /** * @author switch * @copyright 2015 * 管理脚本使用的函数集合 */ function display_category_form( $category = '' ) //显示目录表单 { //如果传入存在目录,进入编辑模式 $edit = is_array ( $category ); ?> <form method= "post" action= "<?php echo $edit ? 'edit_category.php' :'insert_category.php'; ?>" > <table border= "0" > <tr> <td>category name:</td> <td><input type= "text" name= "catname" size= "40" maxlength= "40" value= "<?php echo $edit ? $category['catname'] : ''; ?>" /></td> </tr> <tr> <td <?php if (! $edit ){ echo "colspan=2" ;} ?> align= "center" > <?php if ( $edit ) { echo "<input type=\"hidden\" name=\"catid\" value=\"" . $category [ 'catid' ] . "\" />" ; } ?> <input type= "submit" value= "<?php echo $edit ? 'update' : 'add'; ?> category" /></form> </td> <?php if ( $edit ) //允许删除存在目录 { echo "<td> <form method=\"post\" action=\"delete_category.php\"> <input type=\ "hidden\" name=\"catid\" value=\"" . $category [ 'catid' ] ."\" /> <input type=\"submit\" value=\" delete category\" /> </form></td>"; } ?> </tr> </table> <?php } function display_book_form( $book = '' ) //显示图书表单 { //如果传入图书存在,进入编辑模式 $edit = is_array ( $book ); ?> <form method= "post" action= "<?php echo $edit ? 'edit_book.php' : 'insert_book.php'; ?>" > <table border= "0" > <tr> <td>isbn:</td> <td><input type= "text" name= "isbn" value= "<?php echo $edit ? $book['isbn'] : ''; ?>" /></td> </tr> <tr> <td>book title:</td> <td><input type= "text" name= "title" value= "<?php echo $edit ? $book['title'] : ''; ?>" /></td> </tr> <tr> <td>book author:</td> <td><input type= "text" name= "author" value= "<?php echo $edit ? $book['author'] : ''; ?>" /></td> </tr> <tr> <td>category:</td> <td> <select name= "catid" > <?php $cat_array = get_categories(); foreach ( $cat_array as $thiscat ) { echo "<option value=\"" . $thiscat [ 'catid' ] . "\"" ; if (( $edit ) && ( $thiscat [ 'catid' ] == $book [ 'catid' ])) { echo " selected" ; } echo ">" . $thiscat [ 'catname' ] . "</option>" ; } ?> </select> </td> </tr> <tr> <td>price:</td> <td><input type= "text" name= "price" value= "<?php echo $edit ? $book['price'] : ''; ?>" /></td> </tr> <tr> <td>description:</td> <td><textarea rows= "3" cols= "50" name= "description" ><?php echo $edit ? $book [ 'description' ] : '' ; ?></textarea></td> </tr> <tr> <td <?php if (! $edit ) { echo "colspan=2" ; }?> align= "center" > <?php if ( $edit ) echo "<input type=\"hidden\" name=\"oldisbn\" value=\"" . $book [ 'isbn' ]. "\" />" ;?> <input type= "submit" value= "<?php echo $edit ? 'update' : 'add'; ?> book" /></form></td> <?php if ( $edit ) { echo "<td> <form method=\"post\" action=\"delete_book.php\"> <input type=\ "hidden\" name=\"isbn\" value=\"" . $book [ 'isbn' ]."\" /> <input type=\"submit\" value=\" delete book\"/> </form></td>"; } ?> </td> </tr> </table> </form> <?php } function display_password_form() //显示更改密码表单 { ?> <br /> <form action= "change_password.php" method= "post" > <table width= "250" cellpadding= "2" cellspacing= "0" bgcolor= "#cccccc" > <tr> <td>old password:</td> <td><input type= "password" name= "old_passwd" size= "16" maxlength= "16" /></td> </tr> <tr> <td> new password:</td> <td><input type= "password" name= "new_passwd" size= "16" maxlength= "16" /></td> </tr> <tr> <td>repeat new password:</td> <td><input type= "password" name= "new_passwd2" size= "16" maxlength= "16" /></td> </tr> <tr> <td colspan= "2" align= "center" ><input type= "submit" value= "change password" /></td> </tr> </table> </form> <br /> <?php } function insert_category( $catname ) //目录插入 { $conn = db_connect(); //数据库连接 $query = "select * from categories where catname= '". $catname ."' "; $result = $conn ->query( $query ); if ((! $result ) || ( $result ->num_rows != 0)) return false; $query = "insert into categories values ( '' , '". $catname ."' )"; $result = $conn ->query( $query ); if (! $result ) return false; else return true; } function insert_book( $isbn , $title , $author , $catid , $price , $description ) //图书插入 { $conn = db_connect(); //连接数据库 $query = "select * from books where isbn= '". $isbn ."' "; $result = $conn ->query( $query ); if ((! $result ) || ( $result ->num_rows != 0)) return false; $query = "insert into books values ( '". $isbn ."' , '". $author ."' , '". $title ."' , '". $catid ."' , '". $price ."' , '". $description ."' )"; $result = $conn ->query( $query ); if (! $result ) return false; else return true; } function update_category( $catid , $catname ) //更改目录名称 { $conn = db_connect(); //连接数据库 $query = "update categories set catname= '". $catname ."' where catid= '". $catid ."' "; $result = @ $conn ->query( $query ); if (! $result ) return false; else return true; } function update_book( $oldisbn , $isbn , $title , $author , $catid , $price , $description ) { $conn = db_connect(); //连接数据库 $query = "update books set isbn= '". $isbn ."' , title= '". $title ."' , author= '". $author ."' , catid= '". $catid ."' , price = '". $price ."' , description= '". $description ."' where isbn= '". $oldisbn ."' "; $result = @ $conn ->query( $query ); if (! $result ) return false; else return true; } function delete_category( $catid ) //删除目录 { $conn = db_connect(); //连接数据库 $query = "select * from books where catid= '". $catid ."' "; $result = @ $conn ->query( $query ); if ((! $result ) || (@ $result ->num_rows > 0)) //如果该目录有图书,无法删除该目录 return false; $query = " delete from categories where catid= '". $catid ."' "; $result = @ $conn ->query( $query ); if (! $result ) return false; else return true; } function delete_book( $isbn ) //删除图书 { $conn = db_connect(); //连接数据库 $query = " delete from books where isbn= '". $isbn ."' "; $result = @ $conn ->query( $query ); if (! $result ) return false; else return true; } ?> |
7.10 目录删除操作,图书添加,更新,删除操作基本与上述操作差不多,这里就不在演示,可以下载代码查看
8、扩展
本项目创建了一个相当简单的php购物车系统。我们还可以对它进行许多改进和提高:
- 在真正的在线商店,可能必须建立一些订单记录和实施系统——在这个系统中,用户无法看到已经预定了的订单。
- 顾客希望在不必与我们联系的前提下就能检查到他们的订单处理情况。用户应当可以通过一种身份验证方式使之能够查看自己以前的订单,并且也可以将操作与个人情况紧密地结合起来。也更方便我们收集一些用户习惯信息。
- 图书的图片可以通过ftp之类的服务传输到该网站的图像目录并给它们取一个合适的名字。可以把文件上载到图片插入页,以使该操作方便一些。
- 可以添加用户登录、个性化设置以及书目推荐、在线评论、会员制度、库存级别检查等。可以添加的功能是非常多的。
以上就是php实现购物车功能的全部代码,希望对大家的学习有所帮助。
源码下载:购物车