搜尋
首頁後端開發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://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

<&#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实现购物车功能的全部代码,希望对大家的学习有所帮助。

源码下载:购物车

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP的當前狀態:查看網絡開發趨勢PHP的當前狀態:查看網絡開發趨勢Apr 13, 2025 am 12:20 AM

PHP在現代Web開發中仍然重要,尤其在內容管理和電子商務平台。 1)PHP擁有豐富的生態系統和強大框架支持,如Laravel和Symfony。 2)性能優化可通過OPcache和Nginx實現。 3)PHP8.0引入JIT編譯器,提升性能。 4)雲原生應用通過Docker和Kubernetes部署,提高靈活性和可擴展性。

PHP與其他語言:比較PHP與其他語言:比較Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP與Python:核心功能PHP與Python:核心功能Apr 13, 2025 am 12:16 AM

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHP:網絡開發的關鍵語言PHP:網絡開發的關鍵語言Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP:許多網站的基礎PHP:許多網站的基礎Apr 13, 2025 am 12:07 AM

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

超越炒作:評估當今PHP的角色超越炒作:評估當今PHP的角色Apr 12, 2025 am 12:17 AM

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

PHP中的弱參考是什麼?什麼時候有用?PHP中的弱參考是什麼?什麼時候有用?Apr 12, 2025 am 12:13 AM

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

解釋PHP中的__ Invoke Magic方法。解釋PHP中的__ Invoke Magic方法。Apr 12, 2025 am 12:07 AM

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。