Home > Article > Backend Development > Code example to implement mall SKU function using PHP
Code example of using PHP to implement the mall SKU function
In e-commerce, SKU (Stock Keeping Unit) refers to the inventory unit, which is used to identify and manage specific items. Products of. The SKU function of the mall allows merchants to better manage the inventory and sales of goods, making it easier for customers to select and purchase goods. The following is a code example that uses PHP to implement the mall SKU function.
First, we need to create a database to store product information and SKU information. In this example, we created a data table named "products", which contains basic information about the products. In addition, we created a data table named "skus" to store the SKU information and inventory status of the product.
The structure of the products data table is as follows:
CREATE TABLE `products` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL, `price` DECIMAL(10,2) NOT NULL, `description` TEXT, PRIMARY KEY (`id`) );
The structure of the skus data table is as follows:
CREATE TABLE `skus` ( `id` int(11) NOT NULL AUTO_INCREMENT, `product_id` int(11) NOT NULL, `size` VARCHAR(50) NOT NULL, `color` VARCHAR(50) NOT NULL, `stock` int(11) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`product_id`) REFERENCES `products`(`id`) ON DELETE CASCADE );
Next, we need to create a PHP file to implement the SKU function. First, we need to connect to the database. This can be achieved through PHP's mysqli extension, as shown below:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } ?>
Next, we need to add some functional functions to operate the database. First, we can create a function to get the information of all products, as shown below:
function getProducts() { global $conn; $sql = "SELECT * FROM products"; $result = $conn->query($sql); if ($result->num_rows > 0) { $products = array(); while ($row = $result->fetch_assoc()) { $products[] = $row; } return $products; } else { return null; } }
Then, we can create a function to get all the SKU information of a specific product, as shown below:
function getSkusByProduct($product_id) { global $conn; $sql = "SELECT * FROM skus WHERE product_id = $product_id"; $result = $conn->query($sql); if ($result->num_rows > 0) { $skus = array(); while ($row = $result->fetch_assoc()) { $skus[] = $row; } return $skus; } else { return null; } }
Next, we can create a function to update the inventory of the SKU so that the corresponding inventory is updated after the user purchases the item, as shown below:
function updateStock($sku_id, $quantity) { global $conn; $sql = "UPDATE skus SET stock = stock - $quantity WHERE id = $sku_id"; $conn->query($sql); }
Finally, we can create a function to add a The new SKU is as follows:
function addSku($product_id, $size, $color, $stock) { global $conn; $sql = "INSERT INTO skus (product_id, size, color, stock) VALUES ($product_id, '$size', '$color', $stock)"; $conn->query($sql); }
With the above code example, we can implement the mall SKU function. Developers can expand and optimize accordingly according to actual needs. I hope this example can help you understand how to use PHP to implement the mall SKU function.
The above is the detailed content of Code example to implement mall SKU function using PHP. For more information, please follow other related articles on the PHP Chinese website!