Home > Backend Development > PHP Tutorial > Practical tutorial: Detailed explanation of shopping cart function with PHP and MySQL

Practical tutorial: Detailed explanation of shopping cart function with PHP and MySQL

WBOY
Release: 2024-03-15 12:28:01
Original
1350 people have browsed it

Practical tutorial: Detailed explanation of shopping cart function with PHP and MySQL

Practical tutorial: Detailed explanation of the shopping cart function with PHP and MySQL

The shopping cart function is one of the common functions in website development. Through the shopping cart, users can easily Add the items you want to buy to the shopping cart, then proceed to checkout and payment. In this article, we will detail how to implement a simple shopping cart function using PHP and MySQL and provide specific code examples.

  1. Create database and data table

First you need to create a data table in the MySQL database to store product information. The following is an example of a simple data table structure:

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    image VARCHAR(100) NOT NULL
);
Copy after login
  1. Web interface design

Next, we need to design a simple web interface to display product information and shopping cart functions. The following is an example of a basic HTML interface:

<!DOCTYPE html>
<html>
<head>
    <title>Shopping Cart</title>
</head>
<body>
    <h1>Product list</h1>
    <ul>
        <li>Item 1 - 100 yuan <button onclick="addToCart(1)">Add to cart</button></li>
        <li>Item 2 - 200 yuan <button onclick="addToCart(2)">Add to cart</button></li>
        <li>Item 3 - 300 yuan <button onclick="addToCart(3)">Add to cart</button></li>
    </ul>

    <h1>Shopping Cart</h1>
    <ul id="cart"></ul>

    <script>
        function addToCart(productId) {
            //Add items to shopping cart
        }
    </script>
</body>
</html>
Copy after login
  1. Write PHP code to implement the shopping cart function

In the PHP code, we need to add and remove items from the shopping cart Products and functions such as calculating the total amount of the shopping cart. Here is a simple PHP code example:

<?php
session_start();

//Add items to shopping cart
if(isset($_POST['productId'])){
    $_SESSION['cart'][] = $_POST['productId'];
}

//Remove items from shopping cart
if(isset($_POST['removeIndex'])){
    unset($_SESSION['cart'][$_POST['removeIndex']]);
}

// Calculate the total amount of the shopping cart
$total = 0;
if(isset($_SESSION['cart'])){
    foreach($_SESSION['cart'] as $productId){
        $product = getProductById($productId);
        $total = $product['price'];
    }
}

// Get product information from the database based on the product ID
function getProductById($productId){
    // Connect to the database
    $conn = mysqli_connect("localhost", "root", "", "my_database");

    // Query product information
    $result = mysqli_query($conn, "SELECT * FROM products WHERE id=$productId");
    $product = mysqli_fetch_assoc($result);

    //Close database connection
    mysqli_close($conn);

    return $product;
}
?>
Copy after login
  1. Update the web interface to display the shopping cart information

Finally, we need to update the web interface to display the product information and total amount that the user has selected in the shopping cart section . Here is an example of the updated HTML interface:

<h1>Shopping Cart</h1>
<ul id="cart">
    <?php
    if(isset($_SESSION['cart'])){
        foreach($_SESSION['cart'] as $key => $productId){
            $product = getProductById($productId);
            echo "<li>{$product['name']} - {$product['price']} yuan<button onclick='removeFromCart($key)'>remove</button></ li>";
        }
    }
    ?>
</ul>
<p>Total amount: <?php echo $total; ?>yuan</p>
Copy after login

Through the above steps, we successfully implemented the tutorial of using PHP and MySQL to implement the shopping cart function. Users can browse products on the web page and add them to the shopping cart, and then view the product information and total amount in the shopping cart. In actual projects, the shopping cart function can be expanded and optimized according to needs, such as adding user login and settlement functions. I hope this tutorial is helpful to you, and have a happy programming journey!

The above is the detailed content of Practical tutorial: Detailed explanation of shopping cart function with PHP and MySQL. 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