In this article, we will learn step by step how to build a simple shopping cart function using PHP and MySQL. The shopping cart is an indispensable part of an e-commerce website. It allows users to temporarily store the products they want to purchase and add, delete, modify, and check the products. By studying this article, you will learn how to use PHP to process logic and MySQL to store data to implement a complete shopping cart function.
First, we need to create a database to store product information. Open the MySQL database management tool, create a database namedshopping_cart
, and create a table namedproducts
in it to store product information. Tableproducts
can contain the following fields:
id
: the unique identifier of the product, set as an auto-increment primary keyname
: Product nameprice
: Product priceimage
: Product image pathNext, we need to create a page that displays product information so that users can browse and select products to add to the shopping cart. Create a PHP file namedproducts.php
, query the database to obtain all product information, and display it on the page. Each item should contain an "Add to Cart" button that clicks to add the item to your cart.
query($sql); // Loop through the products and display them on the page if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo ""; } } ?>
When the user clicks the "Add to Cart" button, add the item to the shopping cart. Create a PHP file namedadd_to_cart.php
to handle the logic of adding items to the shopping cart. In this file, you first receive the unique identifier of the item and then add the item to the shopping cart. The shopping cart can be stored using the$_SESSION
variable in PHP.
query($sql); if ($result->num_rows > 0) { $row = $result->fetch_assoc(); // Store product information in the shopping cart $_SESSION['cart'][$product_id] = array( 'name' => $row['name'], 'price' => $row['price'], 'image' => $row['image'], 'quantity' => 1 ); echo "The item has been successfully added to the shopping cart!"; } else { echo "The product does not exist!"; } } ?>
Finally, we need to create a page that displays the shopping cart content and shopping cart operations. Create a PHP file namedcart.php
. By traversing the product information in the shopping cart, display the name, price, quantity and subtotal of each product, and provide the function of adding, reducing and deleting products. .
$product) { echo ""; echo ""; } } else { echo "The shopping cart is empty!"; } ?>".$product['name']."
"; echo ""; echo "Price: $".$product['price']."
"; echo "Quantity: ".$product['quantity']."
"; echo "Subtotal: $".$product['price'] * $product['quantity']."
"; echo "Remove product"; echo "
Through the above steps, we successfully built a simple shopping cart function, which enables the operations of browsing products, adding to the shopping cart, and managing the shopping cart. You can further expand and improve this shopping cart function according to actual needs to make it more complete and practical. I hope this article can be helpful to you, and happy programming!
The above is the detailed content of Learn step by step: How to build a shopping cart function with PHP and MySQL. For more information, please follow other related articles on the PHP Chinese website!