接着上篇继续学习: 《php实现购物车的功能(上)》
7、实现一个管理界面
登录界面
由以下代码实现:
7.1 admin.php
<?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()
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()
function check_admin_user() //检查是否是管理员 { if(isset($_SESSION['admin_user'])) return true; else return false; }
管理主界面
由以下代码实现:
7.4 output_fns.php文件中的函数display_admin_menu()
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="/static/imghwm/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c160211/14551H52X1250-1c228.jpg" class="lazy" src=\"images/". $image .".gif\" alt=\"". $alt ."\" border = \" 0 \" height = \" 50 \" width = \" 135 \" /></a></div>"; }
目录添加
目录添加成功
目录页中可以看出多了Novel目录
由以下代码实现:
7.5 insert_category_form.php
<?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
<?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
<?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
<?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
<?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实现购物车功能的全部代码,希望对大家的学习有所帮助。
源码下载:购物车

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

Fibers在PHP8.1中引入,提升了并发处理能力。1)Fibers是一种轻量级的并发模型,类似于协程。2)它们允许开发者手动控制任务的执行流,适合处理I/O密集型任务。3)使用Fibers可以编写更高效、响应性更强的代码。

PHP社区提供了丰富的资源和支持,帮助开发者成长。1)资源包括官方文档、教程、博客和开源项目如Laravel和Symfony。2)支持可以通过StackOverflow、Reddit和Slack频道获得。3)开发动态可以通过关注RFC了解。4)融入社区可以通过积极参与、贡献代码和学习分享来实现。

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1
好用且免费的代码编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。