Table of Contents
PHP implements the shopping cart function (Part 2), php implements the shopping cart function
Articles you may be interested in:
Home Backend Development PHP Tutorial PHP implements the shopping cart function (Part 2), PHP implements the shopping cart function_PHP tutorial

PHP implements the shopping cart function (Part 2), PHP implements the shopping cart function_PHP tutorial

Jul 12, 2016 am 09:01 AM
json php shopping cart

PHP implements the shopping cart function (Part 2), php implements the shopping cart function

Continue learning from the previous article: "PHP implements the shopping cart function (Part 1)"

7. Implement a management interface


Login interface

is implemented by the following code:
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;> 

Copy after login

7.2 Function login() in the user_auth_fns.php file

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; 
 } 

Copy after login

7.3 Function check_admin_user() in the user_auth_fns.php file

function check_admin_user() //检查是否是管理员 
 { 
 if(isset($_SESSION['admin_user'])) 
 return true; 
 else 
 return false; 
 } 

Copy after login

Management main interface

is implemented by the following code:

7.4 Function display_admin_menu() in the output_fns.php file

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=\"images/". $image .".gif\" 
 alt=\"". $alt ."\" border = \" 0 \" height = \" 50 \" 
 width = \" 135 \" /></a></div>"; 
 } 


Copy after login


Directory added
Directory added successfully
You can see more Novel catalogs on the catalog page

is implemented by the following code:
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;> 

Copy after login

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;> 
Copy after login

Administrator Directory Interface


Directory editing interface - can be updated and deleted


Directory updated successfully


You can see that the directory has been changed successfully on the directory main interface

is implemented by the following code:
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;> 

Copy after login

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;> 
Copy after login

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;>
Copy after login

7.10 Catalog deletion operations, book addition, update, and deletion operations are basically similar to the above operations. They will not be demonstrated here. You can download the code to view

8. Extension
This project creates a fairly simple PHP shopping cart system. There are many improvements and enhancements we can make to it:

  • In a real online store, some order recording and implementation system may have to be established - in this system, users cannot see the orders that have been reserved.
  • Customers want to be able to check on the status of their order without having to contact us. Users should have a means of authentication that enables them to view their previous orders and also allow them to closely tie actions to their personal circumstances. It is also more convenient for us to collect some user habit information.
  • Images of the book can be transferred to the site's image directory via a service such as FTP and given an appropriate name. You can upload files to the image insertion page to make this operation easier.
  • You can add user login, personalized settings, book recommendations, online reviews, membership system, inventory level check, etc. There are so many features that can be added.

The above is all the code to implement the shopping cart function in PHP. I hope it will be helpful to everyone's learning.

Source code download: Shopping cart

Articles you may be interested in:

  • php shopping cart example
  • php shopping cart example (Shenjing)
  • php shopping cart implementation code
  • Sharing of PHP online shopping cart design code
  • PHP update shopping cart quantity (form part/PHP processing part)
  • In-depth PHP shopping cart module function analysis (function explanation, attached Source code)
  • php complete shopping cart implementation code
  • A relatively complete shopping cart class implemented in PHP
  • php uses cookies to implement a shopping cart method
  • php Shopping cart implementation method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1088772.htmlTechArticlephp implements the shopping cart function (Part 2), php implements the shopping cart function. Continue learning from the previous article: "php implements shopping Car Functions (Part 1)》 7. Implement a management interface login interface as follows...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles