Home >Backend Development >PHP Tutorial >PHP development tutorial: Implement multi-specification SKU functions for products from scratch
PHP development tutorial: Implement multi-specification SKU function of products from scratch
Introduction: The multi-specification SKU function of products is very common and essential in e-commerce platforms One of the functions. This article will use code examples to teach you how to implement the multi-specification SKU function of products from scratch.
1. Preparation work
Before starting the implementation, we need to do some preparation work.
2. Create database tables
In this example, we create three tables: product table (product), specification table (specification) and product specification SKU table (product_sku) . Let’s take a look at the table structure first:
-- 商品表(product) CREATE TABLE `product` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '', `create_time` int(11) unsigned DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 规格表(specification) CREATE TABLE `specification` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- 商品规格SKU表(product_sku) CREATE TABLE `product_sku` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `product_id` int(11) unsigned NOT NULL DEFAULT '0', `specification_id` int(11) unsigned NOT NULL DEFAULT '0', `value` varchar(255) NOT NULL DEFAULT '', `price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00', `stock` int(11) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `product_id` (`product_id`), KEY `specification_id` (`specification_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3. Write code
<?php // 创建商品 function createProduct($name) { $db = getDB(); $createTime = time(); $sql = "INSERT INTO `product` (`name`, `create_time`) VALUES (:name, :createTime)"; $statement = $db->prepare($sql); $statement->bindValue(':name', $name); $statement->bindValue(':createTime', $createTime); $statement->execute(); return $db->lastInsertId(); }
<?php // 创建规格 function createSpecification($name) { $db = getDB(); $sql = "INSERT INTO `specification` (`name`) VALUES (:name)"; $statement = $db->prepare($sql); $statement->bindValue(':name', $name); $statement->execute(); return $db->lastInsertId(); }
<?php // 创建商品规格SKU function createProductSku($productId, $specificationId, $value, $price, $stock) { $db = getDB(); $sql = "INSERT INTO `product_sku` (`product_id`, `specification_id`, `value`, `price`, `stock`) VALUES (:productId, :specificationId, :value, :price, :stock)"; $statement = $db->prepare($sql); $statement->bindValue(':productId', $productId); $statement->bindValue(':specificationId', $specificationId); $statement->bindValue(':value', $value); $statement->bindValue(':price', $price); $statement->bindValue(':stock', $stock); $statement->execute(); }
4. Usage example
The following is a simple example that demonstrates how to use the above code to create a product and add multiple specification SKU.
<?php // 创建商品 $productId = createProduct('苹果手机'); // 创建规格 $colorId = createSpecification('颜色'); $capacityId = createSpecification('容量'); // 创建商品规格SKU createProductSku($productId, $colorId, '红色', 5999.00, 10); createProductSku($productId, $colorId, '黑色', 5999.00, 5); createProductSku($productId, $capacityId, '64GB', 5999.00, 3); createProductSku($productId, $capacityId, '128GB', 6999.00, 8);
5. Summary
In this article, we use code examples to realize the creation and use of multi-specification SKU functions for products, and provide a certain foundation for subsequent expansion. Of course, actual projects may involve more complex operations, such as modifying specifications, deleting specifications, etc., and readers can continue to expand on this basis.
I hope this article will help you understand the multi-specification SKU function of the product. thanks for reading!
The above is the detailed content of PHP development tutorial: Implement multi-specification SKU functions for products from scratch. For more information, please follow other related articles on the PHP Chinese website!