搜索
首页后端开发php教程php实现购物车功能(下),php实现购物车功能_PHP教程

php实现购物车功能(下),php实现购物车功能

接着上篇继续学习: 《php实现购物车的功能(上)》

7、实现一个管理界面


登录界面

由以下代码实现:
7.1 admin.php

<&#63;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(); 
&#63;> 

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() //输出管理员菜单 
 { 
 &#63;> 
 <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 /> 
 <&#63;php 
 } 
 
 function display_button($target,$image,$alt) //显示按钮 
 { 
 echo "<div align= \" center \"><a href=\"". $target ."\"> 
 <img src="/static/imghwm/default1.png"  data-src="http://www.bkjia.com/uploads/allimg/160108/0013391T2-2.png"  class="lazy"  src=\"images/". $image .".gif\" 
 alt=\"". $alt ."\" border = \" 0 \" height = \" 50 \" 
 width = \" 135 \" /></a></div>"; 
 } 



目录添加
目录添加成功
目录页中可以看出多了Novel目录

由以下代码实现:
7.5 insert_category_form.php

<&#63;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(); 
&#63;> 

7.6 insert_category.php

<&#63;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(); 
&#63;> 

管理员目录界面


目录编辑界面-可更新,删除


目录更新成功


目录主界面可以看到该目录更改成功

由以下代码实现:
7.7 edit_category_form.php

<&#63;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(); 
&#63;> 

7.8 edit_category.php

<&#63;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(); 
&#63;> 

7.9 admin_fns.php

<&#63;php 
 
/** 
 * @author switch 
 * @copyright 2015 
 * 管理脚本使用的函数集合 
 */ 
 function display_category_form($category = '') //显示目录表单 
 { 
 //如果传入存在目录,进入编辑模式 
 $edit = is_array($category); 
 &#63;> 
 <form method="post" action="<&#63;php echo $edit &#63; 'edit_category.php' :'insert_category.php'; &#63;>"> 
 <table border="0"> 
  <tr> 
  <td>Category Name:</td> 
  <td><input type="text" name="catname" size="40" maxlength="40" value="<&#63;php echo $edit &#63; $category['catname'] : ''; &#63;>"/></td> 
  </tr> 
  <tr> 
  <td <&#63;php if(!$edit){echo "colspan=2";} &#63;> align="center"> 
  <&#63;php 
  if($edit) 
  { 
   echo "<input type=\"hidden\" name=\"catid\" value=\"". $category['catid'] ."\" />"; 
  } 
  &#63;> 
  <input type="submit" value="<&#63;php echo $edit &#63; 'Update' : 'Add'; &#63;> Category"/></form> 
  </td> 
  <&#63;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>"; 
  } 
  &#63;> 
  </tr> 
 </table> 
 <&#63;php 
 } 
 
 function display_book_form($book = '') //显示图书表单 
 { 
 //如果传入图书存在,进入编辑模式 
 $edit = is_array($book); 
 &#63;> 
 
 <form method="post" action="<&#63;php echo $edit &#63; 'edit_book.php' : 'insert_book.php'; &#63;>"> 
 <table border="0"> 
 <tr> 
  <td>ISBN:</td> 
  <td><input type="text" name="isbn" value="<&#63;php echo $edit &#63; $book['isbn'] : ''; &#63;>" /></td> 
 </tr> 
 <tr> 
  <td>Book Title:</td> 
  <td><input type="text" name="title" value="<&#63;php echo $edit &#63; $book['title'] : ''; &#63;>" /></td> 
 </tr> 
 <tr> 
  <td>Book Author:</td> 
  <td><input type="text" name="author" value="<&#63;php echo $edit &#63; $book['author'] : ''; &#63;>"/></td> 
 </tr> 
 <tr> 
  <td>Category:</td> 
  <td> 
  <select name="catid"> 
  <&#63;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>"; 
  } 
  &#63;> 
  </select> 
  </td> 
 </tr> 
 <tr> 
  <td>Price:</td> 
  <td><input type="text" name="price" value="<&#63;php echo $edit &#63; $book['price'] : ''; &#63;>" /></td> 
 </tr> 
 <tr> 
  <td>Description:</td> 
  <td><textarea rows="3" cols="50" name="description"><&#63;php echo $edit &#63; $book['description'] : ''; &#63;></textarea></td> 
 </tr> 
 <tr> 
  <td <&#63;php if (!$edit) { echo "colspan=2"; }&#63;> align="center"> 
  <&#63;php 
  if ($edit) 
  echo "<input type=\"hidden\" name=\"oldisbn\" value=\"".$book['isbn']."\" />";&#63;> 
  <input type="submit" value="<&#63;php echo $edit &#63; 'Update' : 'Add'; &#63;> Book" /></form></td> 
  <&#63;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>"; 
 
  } 
  &#63;> 
  </td> 
 </tr> 
 </table> 
 </form> 
 <&#63;php 
 } 
 
 function display_password_form() //显示更改密码表单 
 { 
 &#63;> 
 <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 /> 
 <&#63;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; 
 } 
&#63;>

 7.10 目录删除操作,图书添加,更新,删除操作基本与上述操作差不多,这里就不在演示,可以下载代码查看

8、扩展
本项目创建了一个相当简单的PHP购物车系统。我们还可以对它进行许多改进和提高:

  • 在真正的在线商店,可能必须建立一些订单记录和实施系统——在这个系统中,用户无法看到已经预定了的订单。
  • 顾客希望在不必与我们联系的前提下就能检查到他们的订单处理情况。用户应当可以通过一种身份验证方式使之能够查看自己以前的订单,并且也可以将操作与个人情况紧密地结合起来。也更方便我们收集一些用户习惯信息。
  • 图书的图片可以通过FTP之类的服务传输到该网站的图像目录并给它们取一个合适的名字。可以把文件上载到图片插入页,以使该操作方便一些。
  • 可以添加用户登录、个性化设置以及书目推荐、在线评论、会员制度、库存级别检查等。可以添加的功能是非常多的。

以上就是php实现购物车功能的全部代码,希望对大家的学习有所帮助。

源码下载:购物车

您可能感兴趣的文章:

  • php 购物车的例子
  • php 购物车实例(申精)
  • php购物车实现代码
  • php网上商城购物车设计代码分享
  • PHP更新购物车数量(表单部分/PHP处理部分)
  • 深入PHP购物车模块功能分析(函数讲解,附源码)
  • php 购物车完整实现代码
  • PHP实现的比较完善的购物车类
  • php利用cookies实现购物车的方法
  • php购物车实现方法

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1088772.htmlTechArticlephp实现购物车功能(下),php实现购物车功能 接着上篇继续学习: 《php实现购物车的功能(上)》 7、实现一个管理界面 登录界面 由以下...
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
使用数据库存储会话的优点是什么?使用数据库存储会话的优点是什么?Apr 24, 2025 am 12:16 AM

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

您如何在PHP中实现自定义会话处理?您如何在PHP中实现自定义会话处理?Apr 24, 2025 am 12:16 AM

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

什么是会话ID?什么是会话ID?Apr 24, 2025 am 12:13 AM

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

您如何在无状态环境(例如API)中处理会议?您如何在无状态环境(例如API)中处理会议?Apr 24, 2025 am 12:12 AM

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。

您如何防止与会议有关的跨站点脚本(XSS)攻击?您如何防止与会议有关的跨站点脚本(XSS)攻击?Apr 23, 2025 am 12:16 AM

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

您如何优化PHP会话性能?您如何优化PHP会话性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

什么是session.gc_maxlifetime配置设置?什么是session.gc_maxlifetime配置设置?Apr 23, 2025 am 12:10 AM

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

您如何在PHP中配置会话名?您如何在PHP中配置会话名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!