Home >Backend Development >PHP Tutorial >Starting from Scratch: A Guide to Development of PHP and MySQL Shopping Cart Functions

Starting from Scratch: A Guide to Development of PHP and MySQL Shopping Cart Functions

王林
王林Original
2024-03-15 16:18:03932browse

Starting from Scratch: A Guide to Development of PHP and MySQL Shopping Cart Functions

"Starting from Scratch: Development Guide for PHP and MySQL Shopping Cart Functions"

In today's era of popular e-commerce, online shopping has become a convenient and fast way consumption patterns. The online shopping cart, as a temporary area for consumers to add products, has become an indispensable part of the e-commerce website. This article will teach readers how to develop a simple PHP and MySQL shopping cart function from scratch, helping readers better understand how online shopping carts are implemented.

Step 1: Create a database

First, we need to create a database named shopping_cart and a table named products, Used to store our product information. In the products table, we can include at least the following fields: id (product ID), name (product name), price ( product price) and image (product image).

CREATE DATABASE shopping_cart;

USE shopping_cart;

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    image VARCHAR(255) NOT NULL
);

Step 2: Create a product page

Next, we need to create a simple product page to display our product information. Here we take a simple HTML page as an example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shopping Cart</title>
</head>
<body>
    <h1>Product list</h1>

    <?php
    $db = new mysqli('localhost', 'root', '', 'shopping_cart');

    $query = $db->query("SELECT * FROM products");
    while($row = $query->fetch_assoc()) {
        echo '
            <div>
                <img src="'.$row['image'].'" alt="'.$row['name'].'"    style="max-width:90%">
                <h3>'.$row['name'].'</h3>
                <p>Price: $'.$row['price'].'</p>
                <a href="add_to_cart.php?id='.$row['id'].'">Add to Cart</a>
            </div>
        ';
    }
    ?>

</body>
</html>

Step Three: Add to Cart Function

Then, we need to create a file named add_to_cart.php to add the product Add to Cart. In this file, we will receive the passed product ID and store it in the Session.

<?php
session_start();

if(isset($_GET['id'])) {
    $id = $_GET['id'];

    if(isset($_SESSION['cart'][$id])) {
        $_SESSION['cart'][$id]['quantity'] ;
    } else {
        $_SESSION['cart'][$id] = array(
            'quantity' => 1
        );
    }

    header('Location: index.php');
    exit;
}
?>

Step 4: Display the shopping cart

Finally, we need to display the product information that has been added to the shopping cart and provide the function of deleting products. Here, we create a file named cart.php to display the shopping cart information.

<?php
session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Shopping Cart</title>
</head>
<body>
    <h1>Shopping Cart</h1>

    <ul>
        <?php
        if(isset($_SESSION['cart'])) {
            foreach($_SESSION['cart'] as $id => $item) {
                $product = $db->query("SELECT * FROM products WHERE id = $id")->fetch_assoc();

                echo '
                    <li>
                        <img src="'.$product['image'].'" alt="'.$product['name'].'"    style="max-width:90%">
                        <h4>'.$product['name'].'</h4>
                        <p>Price: $'.$product['price'].'</p>
                        <p>Quantity: '.$item['quantity'].'</p>
                        <a href="remove_from_cart.php?id='.$id.'">Remove</a>
                    </li>
                ';
            }
        } else {
            echo '<p>Your shopping cart is empty. </p>';
        }
        ?>
    </ul>
</body>
</html>

Summary

Through the above steps, we successfully created a simple PHP and MySQL shopping cart function. Readers can expand and optimize the functions according to their own needs, such as adding total price calculation, settlement page and other functions. I hope this article can help readers better understand the development process of shopping cart functionality.

The above is the detailed content of Starting from Scratch: A Guide to Development of PHP and MySQL Shopping Cart Functions. 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