


PHP basic example: product information management system v1.1, information management system v1.1_PHP tutorial
PHP basic example: product information management system v1.1, information management system v1.1
Achieve the goal: use php and mysql to write a product information management system with Shopping cart function
1. Create database and tables
1. Create database and tables: demodb
2. Create a table: goods
Fields: product number, product name, product type, product picture, unit price, product description, inventory, adding time
2. Create the php file and write the code (the following is the php file to be created and its purpose)
add.php Product addition page
edit.php Product information editing form page
Index.php Product information browsing page
action.php Perform operations such as adding, modifying and deleting product information
dbconfig.php public configuration file, database connection configuration information
Menu.php Website public navigation bar
Uploads/ Storage directory for uploaded images
Function.php public function library file: uploading of image information, scaling and other processing functions
AddCart.php operation of adding shopping cart information (putting purchase information into SESSION)
MyCart.php implements the browsing operation of shopping cart information, and implements the statistics of product information (subtotal and total price)
ClearCart.php implements the operation of deleting a single product or clearing the shopping cart of shopping cart information
UpdateCart.php Modify the number of items in the shopping cart to prevent too small constraints
Illustration of the relationship between each php file:
Okay, here is the code part:
The first is the table creation statement:


The following is the code of each php file. Friends who need it can directly copy each code and put it in the same directory. You must also create an uplaods folder in the same directory to store uploaded images


Publish product information
9 53

View product information"; 53 54 break; 55 case "del": //delete 56 //Get the id number to be deleted and assemble the deletion sql, execute 57 $sql = "delete from goods where id={$_GET['id']}"; 58 59 mysql_query($sql,$link); 60 //Perform image deletion 61 if(mysql_affected_rows($link)>0) 62 { 63 @unlink("./uploads/".$_GET['picname']); 64 @unlink("./uploads/s_".$_GET['picname']); 65 } 66 //Jump to the browsing interface 67 header("Location:index.php"); 68 break; 69 70 case "update": //Modify 71 //1. Get the information to be modified 72 $name = $_POST["name"]; 73 $typeid = $_POST["typeid"]; 74 $price = $_POST["price"]; 75 $total = $_POST["total"]; 76 $note = $_POST["note"]; 77 $id = $_POST['id']; 78 $pic = $_POST['oldpic']; 79 //2. Data verification 80 if(empty($name)) 81 { 82 die("The product name must have a value"); 83 } 84 //3. Determine whether there is an image uploaded 85 if($_FILES['pic']['error']!=4) 86 { 87 //Execute upload 88 $upinfo = uploadFile("pic","./uploads/"); 89 if($upinfo["error"]===false) 90 { 91 die("Picture information upload failed:".$upinfo["info"]); 92 }else 93 { 94 //Upload successful 95 $pic = $upinfo["info"];//Get the name of the successfully uploaded picture 96 //4. Execute scaling when uploading images 97 imageUpdateSize('./uploads/'.$pic,50,50); 98 } 99 } 100 101 102 //5. Execute modifications 103 $sql = "update goods set name='{$name}',typeid={$typeid},price= {$price},total={$total},note='{$note}',pic='{$pic}' where id={$id}"; 104 mysql_query($sql,$link); 105 //6. Determine whether the modification is successful 106 if(mysql_affected_rows($link)>0) 107 { 108 if($_FILES['pic']['error']!=4) 109 { 110 //If there are pictures uploaded, delete the old pictures 111 @unlink("./uploads/".$_POST['oldpic']); 112 @unlink("./uploads/s_".$_POST['oldpic']); 113 } 114 echo "Modification successful" ; 115 }else 116 { 117 echo "Modification failed".mysql_error(); 118 } 119 echo "
View product information"; 120 break; 121 default: 122 echo "Error";break; 123 124 }125 //4. Close the database 126 mysql_close($link); action.php




Browse product information
9 10 11121314151617181920php 21//Read information from the database and output it to the browser table 22 //1. Import configuration file 23require("dbconfig.php"); 24//2. Connect to the database and select the database 25$link = @mysql_connect(HOST,USER,PASS) or die("Database connection failed"); 26mysql_select_db(DBNAME,$link); 27//3. Execute product information query28$sql="select * from goods"; 29$result = mysql_query($sql,$link); 3031//4. Parse product information (parse result set) 32while($row = mysql_fetch_assoc($result)) 33 { 34echo ""; 35echo ""; 36echo ""; 37echo ""; 38echo ""; 39echo ""; 40echo ""; 41echo ""; 47echo ""; 48 } 49//5.释放结果集,关闭数据库50 ?> 51
Item number | Product Name | Product pictures | Unit price | Inventory | Add time | Operation |
---|---|---|---|---|---|---|
{$row["id"]} | {$row["name"]} | {$row["price"]} | {$row["total"]} | ".date("Y-m-d H:i:s",$row['addtime'])." | 42 $row['pic']}'>删除 43 修改 44 放入购物车 45 46 |


编辑商品信息
28 81



Product information management--shopping cart
2 Browse products| 3 Add product| 4 5 My Shopping Cart| 6 Clear shopping cart 7 8 9menu.php


Add items to cart
13 14 php 15 //Read the information to be purchased from the database and add it to the shopping cart 16 //1. Import configuration file 17 require("dbconfig.php"); 18 //2. Connect to the database and select the database 19 $link = @mysql_connect(HOST,USER,PASS) or die("Database connection failed"); 20 mysql_select_db(DBNAME,$link); 21 //3. Perform product information query (obtain the information to be purchased) 22 $sql="select * from goods where id={$_GET['id']}"; 23 $result = mysql_query($sql,$link); 24 25 //4. Determine whether the information you want to purchase is not found, and if so, read and retrieve the information you want to purchase 26 if(empty($result) || mysql_num_rows($result )==0) 27 { 28 die("No information found to buy!"); 29 }else 30 { 31 $shop = mysql_fetch_assoc($result); 32 } 33 $shop["num"]=1;//Add a quantity field 34 //5. Put it in the shopping cart (if the quantity of existing products is accumulated) 35 if(isset($_SESSION["shoplist"]{$shop['id']})) 36 { 37 //If the existing quantity increases by 1 38 $_SESSION["shoplist"][$shop['id']]["num"] ; 39 }else 40 { 41 //If it does not exist, add it to the shopping cart as a newly purchased item 42 $_SESSION["shoplist"][$shop['id']]=$shop; 43 }44 45 ?> 46 47


View my shopping cart
13 14151617181920212223php 24$sum =0;//Variable defining the total amount25if(isset($_SESSION["shoplist"])){ 26foreach($_SESSION["shoplist"] as$v) 27 { 28echo ""; 29echo ""; 30echo ""; 31echo ""; 32echo ""; 33echo ""; 38echo ""; 39echo ""; 40echo ""; 41$sum =$v["price"]*$v['num']; //Accumulated amount42 } 43 }44 ?> 454647484950
Product ID number | Product Name | Product pictures | Unit price | Quantity | Subtotal | Operation |
---|---|---|---|---|---|---|
{$v['id']} | {$v['name']} | {$v['price']} | 34 35 {$v['num']} 36 37 | ".($v["price"]*$v['num '])." | Delete | |
Total amount: | echo $sum; ?> |




The following is a screenshot of index.php:
myCart.php screenshot:
Finally, I would like to say: Hahahahahahahahahahahahahahaha! ! ! !

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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,

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

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

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