How to make a PHP shopping basket
Learn how to create a PHP shopping cart with step-by-step instructions and a working example.
Creating a Shopping Basket with PHP
Creating a shopping basket with PHP is an effective way to provide an online store for customers. With a few simple steps, you can have a functional and secure shopping basket for your website. This tutorial will provide an example of how to create a shopping basket using PHP.
Step 1: Prepare the Database
Before you can begin to create the shopping basket, you need to prepare the database. This includes creating a database table to store the product information, and setting up the connection to the database. To create the database table, use the following SQL code:
CREATE TABLE products (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
price DECIMAL(6,2) NOT NULL
);
This will create a table called “products”, with three columns: “id”, “name”, and “price”. You can then add the product information to the table. Once the database table is created, you can set up the connection to the database using PHP. This can be done with the following code:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Step 2: Create the Shopping Basket
Now that the database is prepared, you can create the shopping basket. The first step is to create a page to display the products. This page will contain a form with a checkbox for each product. The form will also have a “submit” button. The code for this page is as follows:
<form action="basket.php" method="post">
<?php
// Get the products from the database
$sql = "SELECT * FROM products";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo '<input type="checkbox" name="product[]" value="'.$row["id"].'"> '.$row["name"].' - '.$row["price"].'<br>';
}
}
?>
<input type="submit" value="Add to basket">
</form>
The code above will output a form with a checkbox for each product in the database. When the user clicks the “Add to basket” button, the form will be submitted to the “basket.php” page.
The “basket.php” page will be responsible for adding the selected items to the shopping basket. This can be done with the following code:
if (isset($_POST["product"])) {
// Get the selected products
$selected_products = $_POST["product"];
// Add the products to the basket
foreach ($selected_products as $product_id) {
// Get the product details from the database
$sql = "SELECT * FROM products WHERE id = '".$product_id."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
// Add the product to the basket
// (you can put your own code here)
}
}
}
}
The code above will loop through the selected products and get the product details from the database. It will then add the product to the shopping basket (you can put your own code here).
Step 3: Display the Shopping Basket
Once the products are added to the shopping basket, the next step is to display the basket contents. This can be done with the following code:
// Get the products from the basket
$sql = "SELECT * FROM basket";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo '<p>'.$row["name"].' - '.$row["price"].'</p>';
}
}
The code above will output a list of the products in the basket. This can then be used to display the basket contents to the user.
Conclusion
Creating a shopping basket with PHP is a relatively straightforward process. By following the steps outlined in this tutorial, you can create a functional and secure shopping basket for your website.