Home > Backend Development > PHP Tutorial > How to use PHP programming language to create product multi-specification SKU function

How to use PHP programming language to create product multi-specification SKU function

WBOY
Release: 2023-09-05 11:30:01
Original
997 people have browsed it

How to use PHP programming language to create product multi-specification SKU function

How to use PHP programming language to create product multi-specification SKU function

With the prosperity and development of e-commerce, the increase in product diversity and specifications has become a problem faced by merchants. an important question. In order to better manage product information and provide more purchasing options, we need to implement the multi-specification SKU function of the product. This article will introduce how to use the PHP programming language to create multi-specification SKU functions for products, and provide relevant code examples.

1. Design the database table structure

Before we start writing PHP code, we need to design the database table structure first. We can create two tables, one is a product table, used to store basic information about products; the other is a SKU table, used to store multi-specification information about products.

The structure of the product table is as follows:

CREATE TABLE goods (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
price decimal(10,2) NOT NULL,
PRIMARY KEY (id)
) ;

The structure of the SKU table is as follows:

CREATE TABLE sku (
id int(11) NOT NULL AUTO_INCREMENT,
goods_id int(11) NOT NULL,
sku_name varchar(255) NOT NULL,
sku_price decimal(10,2) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (goods_id) REFERENCES goods (id)
) ;

2. Write PHP code

Now we can start writing PHP code. First, we need to connect to the database, and then write some functions to query product and SKU information, insert product and SKU information, etc.

  1. Connect to the database

$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn- >connect_error) {

die("Connection failed: " . $conn->connect_error);
Copy after login

}
?>

Please replace your_username, your_password and your_database above with your own database username, password and database name.

  1. Query product information

We can write a function to query product information:

function getGoods($conn) {

$sql = "SELECT * FROM goods";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Price: $" . $row["price"]. "<br>";
    }
} else {
    echo "No goods found";
}
Copy after login

}
?>

  1. Query SKU information

We can write a function to query SKU information:

function getSKU($conn, $goods_id) {

$sql = "SELECT * FROM sku WHERE goods_id = $goods_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - SKU Name: " . $row["sku_name"]. " - SKU Price: $" . $row["sku_price"]. "<br>";
    }
} else {
    echo "No SKU found";
}
Copy after login

}
?>

  1. Insert product information

We can write a function to insert product information:

function insertGoods($conn, $name, $price) {

$sql = "INSERT INTO goods (name, price) VALUES ('$name', '$price')";

if ($conn->query($sql) === TRUE) {
    echo "Goods inserted successfully";
} else {
    echo "Error inserting goods: " . $conn->error;
}
Copy after login

}
?>

  1. Insert SKU information

We can write a function to insert SKU information:

function insertSKU ($conn, $goods_id, $sku_name, $sku_price) {

$sql = "INSERT INTO sku (goods_id, sku_name, sku_price) VALUES ('$goods_id', '$sku_name', '$sku_price')";

if ($conn->query($sql) === TRUE) {
    echo "SKU inserted successfully";
} else {
    echo "Error inserting SKU: " . $conn->error;
}
Copy after login

}
?>

3. Usage examples

The following are some usage examples, Demonstrates how to query product information and SKU information, and how to insert product information and SKU information.

  1. Query product information

getGoods($conn);
?>

  1. Query SKU information

getSKU($conn, 1); // Query the SKU information of product ID 1
?>

  1. Insert product information

insertGoods($conn, "iPhone 12", 999.99);
?>

  1. Insert SKU Information

insertSKU($conn, 1, "64GB", 999.99);
insertSKU($conn, 1, "128GB", 1099.99);
?>

The above example code can be run by copying the relevant code into a PHP file and accessing the file in the browser.

Through the above steps, we have successfully created the multi-specification SKU function of the product. You can continue to improve the code according to your own needs and implement more complex functions, such as updating product information, deleting product and SKU information, etc.

Summary

This article introduces how to use the PHP programming language to create multi-specification SKU functions for products, and provides relevant code examples. By learning and mastering these knowledge and skills, we can better manage product information, provide more purchasing options, and improve the user experience of e-commerce. Hope this article helps you!

The above is the detailed content of How to use PHP programming language to create product multi-specification SKU function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template