Online Shopping System using PHP, MySQL with Free Source Code
Online Shopping System using PHP, MySQL with Free Source Code

Online Shopping System using PHP, MySQL with Free Source Code


Introduction

The online shopping world is vast, growing, and incredibly dynamic. Every time you shop online, whether you’re browsing for a book or the latest tech gadgets, a powerful system works behind the scenes to ensure that everything runs smoothly. At the core of these systems lie powerful web development technologies like PHP, CSS, JavaScript (JS), and MySQL.

Why Use PHP, CSS, JS, and MySQL for an Online Shopping System?

PHP: The Heart of Backend Development

PHP is the server-side scripting language responsible for the backend operations of an online shopping system. It processes user requests, manages product information, handles form submissions, and communicates with the MySQL database. It’s like the brain behind the system, controlling everything that happens on the server.

Imagine browsing through a catalog of products. PHP is responsible for fetching product data from the database, showing the details to you, and processing any action you take—like adding an item to your cart.

CSS: Creating a Beautiful and Engaging UI

While PHP manages the back-end, CSS is what makes your online shopping system look visually appealing. CSS handles the styling of your web pages—from colors and fonts to layout and responsiveness. Without CSS, your website would be nothing more than a plain, text-filled page.

CSS helps you create a sleek, professional design that engages users and enhances their experience. It’s the artist of your online platform!

JavaScript: Adding Interactivity and Real-Time Updates

If PHP is the brain, then JavaScript is the hands of the system. JavaScript makes the system interactive and responsive. With JS, you can create features like real-time product search, form validation, and dynamic content updates without needing to reload the entire page. This ensures a smoother experience for the user, keeping them engaged and on your site longer.

See also  Hospital Billing Management System in Python

MySQL: Managing and Storing Your Data Efficiently

Finally, MySQL is where all the data is stored. Think of it as the library of your online shopping platform. MySQL manages product details, user information, order histories, and much more. It works in harmony with PHP to fetch and update data in real-time.

Blood Pressure Monitoring Management System Using PHP and MySQL with Guide

Features

An online shopping system built using PHP, CSS, JS, and MySQL can include various key features to ensure it functions efficiently:

  • Product Listings and Categories: Organize products in categories for easy browsing.
  • Shopping Cart and Checkout Process: Allow users to add items to their cart and complete their purchase with ease.
  • User Registration and Authentication: Secure login and registration processes ensure user data is protected.
  • Payment Gateway Integration: Securely process payments through various gateways like PayPal or Stripe.
  • Order History and User Dashboard: Let users view past orders and manage their account details.

Setting Up

To get started, you’ll need the right tools. Most developers use XAMPP as their local development environment because it comes with Apache, PHP, and MySQL pre-installed. You’ll also need a good text editor like VS Code to write your code.

Installing XAMPP

  1. Download XAMPP from the official website.
  2. Install it on your computer and ensure Apache and MySQL services are running.
  3. Start creating your online shopping platform by writing PHP scripts and designing the front end with CSS and JS.
  4. Designing your MySQL database is one of the most important steps in building an online shopping system. You’ll need tables for:
  • Users: Store user registration details.
  • Products: Include product names, prices, descriptions, and stock information.
  • Orders: Track purchases, including order numbers, user IDs, and payment status.

Source Code


1. Database (MySQL)

The foundation of your online shopping system is the database. This is where we store all of the essential data: product details, user information, and order records.

Database Schema (MySQL)

CREATE DATABASE online_shop;

USE online_shop;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    password VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    product_name VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    description TEXT,
    image_url VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    total DECIMAL(10, 2) NOT NULL,
    order_status VARCHAR(50) NOT NULL DEFAULT 'Pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE order_items (
    id INT AUTO_INCREMENT PRIMARY KEY,
    order_id INT NOT NULL,
    product_id INT NOT NULL,
    quantity INT NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    FOREIGN KEY (order_id) REFERENCES orders(id),
    FOREIGN KEY (product_id) REFERENCES products(id)
);

2. User Registration and Login (PHP)

The next step is to build the backend logic for user registration and login. Users need to sign up and log in to make purchases.

See also  Data Types in PHP

User Registration (register.php)

<?php
include 'config.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = password_hash($_POST['password'], PASSWORD_BCRYPT);

    $sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
    if (mysqli_query($conn, $sql)) {
        echo "Registration successful!";
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}
?>

<!-- HTML form -->
<form method="POST" action="register.php">
    <input type="text" name="username" placeholder="Username" required>
    <input type="email" name="email" placeholder="Email" required>
    <input type="password" name="password" placeholder="Password" required>
    <button type="submit">Register</button>
</form>

User Login (login.php)

<?php
include 'config.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = $_POST['email'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM users WHERE email='$email'";
    $result = mysqli_query($conn, $sql);
    $user = mysqli_fetch_assoc($result);

    if ($user && password_verify($password, $user['password'])) {
        session_start();
        $_SESSION['user_id'] = $user['id'];
        echo "Login successful!";
    } else {
        echo "Invalid credentials!";
    }
}
?>

<!-- HTML form -->
<form method="POST" action="login.php">
    <input type="email" name="email" placeholder="Email" required>
    <input type="password" name="password" placeholder="Password" required>
    <button type="submit">Login</button>
</form>

3. Product Listing (PHP)

Displaying the products is a key part of the shopping system. Here’s the PHP code for fetching and displaying products from the database.

Display Products (products.php)

<?php
include 'config.php';

$sql = "SELECT * FROM products";
$result = mysqli_query($conn, $sql);
?>

<div class="product-list">
    <?php while ($row = mysqli_fetch_assoc($result)): ?>
        <div class="product">
            <h2><?php echo $row['product_name']; ?></h2>
            <p><?php echo $row['description']; ?></p>
            <p>Price: $<?php echo $row['price']; ?></p>
            <img src="<?php echo $row['image_url']; ?>" alt="Product Image">
            <a href="cart.php?action=add&id=<?php echo $row['id']; ?>">Add to Cart</a>
        </div>
    <?php endwhile; ?>
</div>

4. Shopping Cart (PHP)

The shopping cart allows users to add products, adjust quantities, and proceed to checkout.

Add to Cart (cart.php)

<?php
session_start();
include 'config.php';

if ($_GET['action'] == 'add') {
    $product_id = $_GET['id'];
    $sql = "SELECT * FROM products WHERE id='$product_id'";
    $result = mysqli_query($conn, $sql);
    $product = mysqli_fetch_assoc($result);

    if (isset($_SESSION['cart'][$product_id])) {
        $_SESSION['cart'][$product_id]['quantity']++;
    } else {
        $_SESSION['cart'][$product_id] = array(
            "name" => $product['product_name'],
            "price" => $product['price'],
            "quantity" => 1
        );
    }

    header("Location: cart.php");
}
?>

<!-- HTML for cart display -->
<div class="cart">
    <?php if (!empty($_SESSION['cart'])): ?>
        <?php foreach ($_SESSION['cart'] as $product_id => $details): ?>
            <div class="cart-item">
                <p><?php echo $details['name']; ?> (<?php echo $details['quantity']; ?>)</p>
                <p>Price: $<?php echo $details['price'] * $details['quantity']; ?></p>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

5. Checkout and Order

Once the user has added products to their cart, they can checkout and complete the purchase. The code below processes the order and saves it to the database.

See also  Basic Snake Game in Python with Source Code

Checkout (checkout.php)

<?php
session_start();
include 'config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $user_id = $_SESSION['user_id'];
    $total = $_POST['total'];

    $sql = "INSERT INTO orders (user_id, total) VALUES ('$user_id', '$total')";
    if (mysqli_query($conn, $sql)) {
        $order_id = mysqli_insert_id($conn);

        foreach ($_SESSION['cart'] as $product_id => $details) {
            $quantity = $details['quantity'];
            $price = $details['price'];
            mysqli_query($conn, "INSERT INTO order_items (order_id, product_id, quantity, price) VALUES ('$order_id', '$product_id', '$quantity', '$price')");
        }

        unset($_SESSION['cart']);
        echo "Order placed successfully!";
    } else {
        echo "Error: " . mysqli_error($conn);
    }
}
?>

<!-- HTML for checkout form -->
<form method="POST" action="checkout.php">
    <p>Total: $<?php echo $_SESSION['total']; ?></p>
    <input type="hidden" name="total" value="<?php echo $_SESSION['total']; ?>">
    <button type="submit">Place Order</button>
</form>

6. CSS:

The CSS below will give your online shopping platform a clean and modern look, ensuring users have a seamless experience.

body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    color: #333;
}

.product-list {
    display: flex;
    flex-wrap: wrap;
}

.product {
    background-color: #fff;
    border: 1px solid #ddd;
    padding: 20px;
    margin: 10px;
    width: 250px;
    text-align: center;
}

.product img {
    max-width: 100%;
    height: auto;
}

.cart {
    margin: 20px 0;
}

.cart-item {
    background-color: #fff;
    padding: 10px;
    margin-bottom: 10px;
}

online shopping system using php mysql source code
online shopping system using php mysql pdf
online shopping system using php mysql free download
online shopping System in php mysql full source code
online shopping system using php mysql free
online shopping system using php mysql github
Online Shopping System using php mysql full source code github
online shopping website in php source code

Show 1 Comment

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *