search
HomeBackend DevelopmentPHP TutorialCode 2 about implementing shopping cart in php

Code 2 about implementing shopping cart in php

Jun 19, 2018 pm 05:25 PM
phpshopping cart

This article mainly introduces all the codes for implementing the shopping cart function in PHP, and proposes demand analysis, solutions, and database creation to help you easily implement the shopping cart function. Interested friends can refer to it

Continue learning from the previous article: "About the code for implementing shopping cart in PHP 1"

7. Implement a management interface


Login interface

is implemented by the following code:
7.1 admin.php

<?php 
 
/** 
 * @author switch 
 * @copyright 2015 
 * 主管理菜单 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
 require_once(&#39;book_sc_fns.php&#39;); 
 
 session_start(); 
 
 if((@$_POST[&#39;username&#39;]) && (@$_POST[&#39;passwd&#39;])) //尝试登陆 
 { 
 $username = $_POST[&#39;username&#39;]; 
 $passwd = $_POST[&#39;passwd&#39;]; 
 
 if(login($username,$passwd)) 
 { 
 $_SESSION[&#39;admin_user&#39;] = $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(&#39;login.php&#39;,&#39;Login&#39;); 
 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(&#39;login.php&#39;,&#39;Login&#39;); 
 } 
 do_html_footer(); 
?>

7.2 The 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=&#39;". $username ."&#39; 
  and password = sha1(&#39;". $password ."&#39;)"; 
 $result = $conn ->query($query); 
 
 if(!$result) 
 return 0; 
 
 if($result ->num_rows > 0) 
 return 1; 
 else 
 return 0; 
 }

7.3 The function check_admin_user() in the user_auth_fns.php file

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

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() //输出管理员菜单 
 { 
 ?> 
 <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 "<p align= \" center \"><a href=\"". $target ."\"> 
 <img src="/static/imghwm/default1.png"  data-src="https://img.php.cn/upload/article/000/000/009/17e15b5ed616815d0f41e5053888cea1-2.png?x-oss-process=image/resize,p_40"  class="lazy"  src=\"images/". $image .".gif\" 
 alt=\"". $alt ."\" border = \" 0 \" height = \" 50 \" 
 width = \" 135 \" /></a></p>"; 
 }


Directory addition
Directory added successfully
You can see more Novel directories on the directory page

Achieved by the following code :
7.5 insert_category_form.php

<?php 
 
/** 
 * @author switch 
 * @copyright 2015 
 * 允许管理员向数据库中添加一个目录的表格 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含 
 require_once(&#39;book_sc_fns.php&#39;); 
 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(&#39;book_sc_fns.php&#39;); 
 session_start(); 
 
 do_html_header("Adding a category"); 
 if(check_admin_user()) 
 { 
 if(filled_out($_POST)) 
 { 
 $catname =$_POST[&#39;catname&#39;]; 
 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(); 
?>

Administrator directory interface


##Directory editing interface-updatable, Delete


Directory update successful


The directory main interface can see that the directory has been changed successfully


is implemented by the following code:

7.7 edit_category_form.php


<?php 
 
/** 
 * @author switch 
 * @copyright 2015 
 * 管理员编辑目录的表单 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
 require_once(&#39;book_sc_fns.php&#39;); 
 session_start(); 
 
 do_html_header("Edit category"); 
 if(check_admin_user()) 
 { 
 if($catname = get_category_name($_GET[&#39;catid&#39;])) 
 { 
 $catid = $_GET[&#39;catid&#39;]; 
 $cat = compact(&#39;catname&#39;,&#39;catid&#39;); 
 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(&#39;book_sc_fns.php&#39;); 
 session_start(); 
 
 do_html_header("Updating category"); 
 if(check_admin_user()) 
 { 
 if(filled_out($_POST)) 
 { 
 if(update_category($_POST[&#39;catid&#39;],$_POST[&#39;catname&#39;])) 
 { 
 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 = &#39;&#39;) //显示目录表单 
 { 
 //如果传入存在目录,进入编辑模式 
 $edit = is_array($category); 
 ?> 
 <form method="post" action="<?php echo $edit ? &#39;edit_category.php&#39; :&#39;insert_category.php&#39;; ?>"> 
 <table border="0"> 
  <tr> 
  <td>Category Name:</td> 
  <td><input type="text" name="catname" size="40" maxlength="40" value="<?php echo $edit ? $category[&#39;catname&#39;] : &#39;&#39;; ?>"/></td> 
  </tr> 
  <tr> 
  <td <?php if(!$edit){echo "colspan=2";} ?> align="center"> 
  <?php 
  if($edit) 
  { 
   echo "<input type=\"hidden\" name=\"catid\" value=\"". $category[&#39;catid&#39;] ."\" />"; 
  } 
  ?> 
  <input type="submit" value="<?php echo $edit ? &#39;Update&#39; : &#39;Add&#39;; ?> Category"/></form> 
  </td> 
  <?php 
  if($edit) //允许删除存在目录 
  { 
  echo "<td> 
   <form method=\"post\" action=\"delete_category.php\"> 
   <input type=\"hidden\" name=\"catid\" value=\"". $category[&#39;catid&#39;] ."\" /> 
   <input type=\"submit\" value=\"Delete category\" /> 
   </form></td>"; 
  } 
  ?> 
  </tr> 
 </table> 
 <?php 
 } 
 
 function display_book_form($book = &#39;&#39;) //显示图书表单 
 { 
 //如果传入图书存在,进入编辑模式 
 $edit = is_array($book); 
 ?> 
 
 <form method="post" action="<?php echo $edit ? &#39;edit_book.php&#39; : &#39;insert_book.php&#39;; ?>"> 
 <table border="0"> 
 <tr> 
  <td>ISBN:</td> 
  <td><input type="text" name="isbn" value="<?php echo $edit ? $book[&#39;isbn&#39;] : &#39;&#39;; ?>" /></td> 
 </tr> 
 <tr> 
  <td>Book Title:</td> 
  <td><input type="text" name="title" value="<?php echo $edit ? $book[&#39;title&#39;] : &#39;&#39;; ?>" /></td> 
 </tr> 
 <tr> 
  <td>Book Author:</td> 
  <td><input type="text" name="author" value="<?php echo $edit ? $book[&#39;author&#39;] : &#39;&#39;; ?>"/></td> 
 </tr> 
 <tr> 
  <td>Category:</td> 
  <td> 
  <select name="catid"> 
  <?php 
  $cat_array = get_categories(); 
  foreach($cat_array as $thiscat) 
  { 
   echo "<option value=\"". $thiscat[&#39;catid&#39;] ."\""; 
   if(($edit) && ($thiscat[&#39;catid&#39;] == $book[&#39;catid&#39;])) 
   { 
   echo " selected"; 
   } 
   echo ">". $thiscat[&#39;catname&#39;] ."</option>"; 
  } 
  ?> 
  </select> 
  </td> 
 </tr> 
 <tr> 
  <td>Price:</td> 
  <td><input type="text" name="price" value="<?php echo $edit ? $book[&#39;price&#39;] : &#39;&#39;; ?>" /></td> 
 </tr> 
 <tr> 
  <td>Description:</td> 
  <td><textarea rows="3" cols="50" name="description"><?php echo $edit ? $book[&#39;description&#39;] : &#39;&#39;; ?></textarea></td> 
 </tr> 
 <tr> 
  <td <?php if (!$edit) { echo "colspan=2"; }?> align="center"> 
  <?php 
  if ($edit) 
  echo "<input type=\"hidden\" name=\"oldisbn\" value=\"".$book[&#39;isbn&#39;]."\" />";?> 
  <input type="submit" value="<?php echo $edit ? &#39;Update&#39; : &#39;Add&#39;; ?> Book" /></form></td> 
  <?php 
  if ($edit) 
  { 
  echo "<td> 
   <form method=\"post\" action=\"delete_book.php\"> 
   <input type=\"hidden\" name=\"isbn\" value=\"".$book[&#39;isbn&#39;]."\" /> 
   <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=&#39;". $catname ."&#39;"; 
 $result = $conn ->query($query); 
 if((!$result) || ($result ->num_rows != 0)) 
 return false; 
 
 $query = "insert into categories values 
 (&#39;&#39;,&#39;". $catname ."&#39;)"; 
 $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=&#39;". $isbn ."&#39;"; 
 $result = $conn ->query($query); 
 if((!$result) || ($result ->num_rows != 0)) 
 return false; 
 
 $query = "insert into books values 
 (&#39;". $isbn ."&#39;,&#39;". $author ."&#39;,&#39;". $title ."&#39;, 
 &#39;". $catid ."&#39;,&#39;". $price ."&#39;,&#39;". $description ."&#39;)"; 
 
 
 $result = $conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
 
 function update_category($catid,$catname) //更改目录名称 
 { 
 $conn = db_connect(); //连接数据库 
 
 $query = "update categories 
  set catname=&#39;". $catname ."&#39; 
  where catid=&#39;". $catid ."&#39;"; 
 $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=&#39;". $isbn ."&#39;, 
  title=&#39;". $title ."&#39;, 
  author=&#39;". $author ."&#39;, 
  catid=&#39;". $catid ."&#39;, 
  price =&#39;". $price ."&#39;, 
  description=&#39;". $description ."&#39; 
  where isbn=&#39;". $oldisbn ."&#39;"; 
 $result = @$conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
 
 function delete_category($catid) //删除目录 
 { 
 $conn = db_connect(); //连接数据库 
 
 $query = "select * 
  from books 
  where catid=&#39;". $catid ."&#39;"; 
 $result = @$conn ->query($query); 
 if((!$result) || (@$result ->num_rows > 0)) //如果该目录有图书,无法删除该目录 
 return false; 
 
 $query = "delete from categories 
  where catid=&#39;". $catid ."&#39;"; 
 $result = @$conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
 
 function delete_book($isbn) //删除图书 
 { 
 $conn = db_connect(); //连接数据库 
 
 $query = "delete from books 
  where isbn=&#39;". $isbn ."&#39;"; 
 $result = @$conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
?>

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 was created A fairly simple PHP shopping cart system. We can also make many improvements and enhancements to it:


  • # In a real online store, some order recording and implementation system may have to be established - in this system , the user cannot see the orders that have been booked.

  • 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.

  • Pictures of the book can be transferred to the site's images 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 and book recommendations, online reviews, membership systems, inventory level checks, etc. There are so many features that can be added.

  • The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the code for implementing the shopping cart in PHP 1


##

The above is the detailed content of Code 2 about implementing shopping cart in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment