Home  >  Article  >  Backend Development  >  How to use the PHP Developer City function: add products to the shopping cart

How to use the PHP Developer City function: add products to the shopping cart

WBOY
WBOYOriginal
2023-07-29 08:34:491596browse

How to use PHP to develop the mall function: add products to the shopping cart

Today’s e-commerce industry is booming, and more and more companies choose to open shopping malls on the Internet. When building a practical and stable shopping mall platform, adding products to the shopping cart is a very basic and important function. This article will introduce how to use the function of adding products to the shopping cart in the PHP developer mall function, and provide corresponding code examples.

  1. Create a shopping cart table

First, we need to create a shopping cart table in the database to save the product information added to the shopping cart by the user. The structure of the shopping cart table can contain the following fields: cart_id (shopping cart ID), user_id (user ID), product_id (product ID), quantity (product quantity), price (product price), etc.

You can use the following SQL statement to create a shopping cart table in the database:

CREATE TABLE `cart` (
    `cart_id` INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT(10) UNSIGNED,
    `product_id` INT(10) UNSIGNED,
    `quantity` INT(10) UNSIGNED,
    `price` DECIMAL(10, 2)
);
  1. Add items to the shopping cart

On the front-end interface, click "Add to Cart" button allows us to add items to the shopping cart. In PHP development, the following code can be used to handle the request to add items to the shopping cart:

// 接收前端传递的参数
$user_id = $_POST['user_id'];
$product_id = $_POST['product_id'];
$quantity = $_POST['quantity'];
$price = $_POST['price'];

// 插入购物车记录
$sql = "INSERT INTO cart (user_id, product_id, quantity, price) VALUES ('$user_id', '$product_id', '$quantity', '$price')";
$result = mysqli_query($connection, $sql);

// 判断插入是否成功
if ($result) {
    echo "商品已成功添加到购物车!";
} else {
    echo "添加商品到购物车失败,请稍后重试!";
}
  1. Display the list of items in the shopping cart

Generally speaking, users After adding items to the shopping cart, you will want to view the list of items in the shopping cart. In PHP, we can display the product list in the shopping cart through the following code:

// 查询购物车中的商品列表
$sql = "SELECT cart.cart_id, product.name, cart.quantity, cart.price FROM cart LEFT JOIN product ON cart.product_id = product.product_id WHERE cart.user_id = '$user_id'";
$result = mysqli_query($connection, $sql);

// 遍历并展示商品列表
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo "商品名称:" . $row['name'] . "<br>";
        echo "数量:" . $row['quantity'] . "<br>";
        echo "价格:" . $row['price'] . "<br>";
        echo "<hr>";
    }
} else {
    echo "购物车中没有商品!";
}

Through the above code, we can query the product information in the shopping cart and display it on the front-end interface to make it convenient for users View the list of items in your shopping cart.

Summary

This article introduces how to use the function of adding products to the shopping cart in the PHP Developer City function, and provides corresponding code examples. By creating a shopping cart table, handling requests to add items to the shopping cart, and displaying the list of items in the shopping cart, we are able to build a stable and practical mall platform. I hope this article can be helpful to developers who are new to PHP mall development.

The above is the detailed content of How to use the PHP Developer City function: add products to the shopping cart. 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