Home > Article > Backend Development > 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.
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 );
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>
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; } ?>
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>
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!