├── 00_documentation.md ├── 01_databaseSetup.md ├── 02_registrationPageSetup.md ├── 03_loginPageSetup.md ├── 04_logoutPageSetup.md ├── 05_indexPageSetup.md ├── 06_cartPageSetup.md ├── 07_adminLoginPageSetup.md ├── 08_adminLogoutPageSetup.md ├── 09_adminDashboardPageSetup.md ├── 10_adminManageProductsPageSetup.md ├── 11_adminAddProductPageSetup.md ├── admin ├── add_product.php ├── dashboard.php ├── login.php ├── logout.php └── manage_products.php ├── css └── style.css ├── images ├── cart-icon.png ├── product1.jpg ├── product2.jpg ├── product3.jpg ├── product4.jpg └── product5.png ├── includes └── db.php ├── index.php └── pages ├── cart.php ├── login.php ├── logout.php └── register.php /00_documentation.md: -------------------------------------------------------------------------------- 1 | ### **Project Overview** 2 | This e-commerce platform is designed for **Customers** and **Admins** to handle shopping and management functionalities. Below are concise, sectioned explanations for each part of the project. 3 | 4 | --- 5 | 6 | ### **1. User Section** 7 | This section explains how users interact with the website. 8 | 9 | #### **1.1. How can users register on the platform?** 10 | - **Answer**: Users can create an account on the `register.php` page by providing their name, email, and password. This allows them to access personalized features like the shopping cart. 11 | 12 | #### **1.2. How can users log in and log out?** 13 | - **Login**: Users log in on the `login.php` page using their email and password. 14 | - **Logout**: The `logout.php` page ends the session, ensuring user data is secure. 15 | 16 | #### **1.3. How can users view products?** 17 | - **Answer**: All products are displayed on the homepage (`index.php`). Users can see product names, prices, descriptions, and images. 18 | 19 | #### **1.4. How can users add products to their cart?** 20 | - **Answer**: On the `index.php` page, users click "Add to Cart" to save items. The system records these items in the cart for the logged-in user. 21 | 22 | #### **1.5. How can users manage their cart?** 23 | - **Answer**: The `cart.php` page lets users view, update, or remove items from their cart. It also shows the total cost of selected products. 24 | 25 | --- 26 | 27 | ### **2. Admin Section** 28 | This section explains how admins manage the platform. 29 | 30 | #### **2.1. How do admins log in and log out?** 31 | - **Login**: Admins log in via `admin/login.php` using their credentials. 32 | - **Logout**: The `admin/logout.php` page ends the admin session securely. 33 | 34 | #### **2.2. What is the admin dashboard?** 35 | - **Answer**: The `admin/dashboard.php` page provides a control panel with options to add, edit, and delete products. 36 | 37 | #### **2.3. How do admins add products?** 38 | - **Answer**: The `admin/add_product.php` page allows admins to add new products by filling out a form with product details and uploading an image. 39 | 40 | #### **2.4. How do admins manage products?** 41 | - **Answer**: The `admin/manage_products.php` page shows all products in a table with options to: 42 | - Edit product details. 43 | - Delete products from the store. 44 | 45 | --- 46 | 47 | ### **3. Database Section** 48 | This section explains how the database is structured. 49 | 50 | #### **3.1. What does the users table do?** 51 | - **Answer**: The `users` table stores information about all users (customers and admins): 52 | - Usernames, emails, hashed passwords. 53 | - A `role` field to differentiate between customers (`user`) and admins (`admin`). 54 | 55 | #### **3.2. What does the products table do?** 56 | - **Answer**: The `products` table contains product details: 57 | - Names, prices, descriptions, and image filenames. 58 | 59 | #### **3.3. What does the cart table do?** 60 | - **Answer**: The `cart` table keeps track of items added to users' carts: 61 | - Links users to products and stores the quantity of each item. 62 | 63 | --- 64 | 65 | ### **4. Flow of the Website** 66 | This section explains the flow of user actions. 67 | 68 | #### **4.1. What happens when a user registers?** 69 | - **Answer**: The system validates the input, hashes the password, and saves the user data in the `users` table. 70 | 71 | #### **4.2. What happens when a user logs in?** 72 | - **Answer**: The system verifies the email and password, then starts a session to allow personalized access. 73 | 74 | #### **4.3. What happens when a product is added to the cart?** 75 | - **Answer**: The system saves the product details in the `cart` table, linked to the logged-in user's ID. 76 | 77 | #### **4.4. What happens when an admin adds a product?** 78 | - **Answer**: The system uploads the product image, saves the product details in the `products` table, and displays it on the homepage. 79 | 80 | #### **4.5. What happens when an admin deletes a product?** 81 | - **Answer**: The system removes the product entry from the `products` table, making it unavailable on the website. 82 | 83 | --- 84 | 85 | ### **5. Security Measures** 86 | This section explains how the website ensures security. 87 | 88 | #### **5.1. How are passwords secured?** 89 | - **Answer**: Passwords are hashed using `password_hash()` before being stored in the database. 90 | 91 | #### **5.2. How is session management handled?** 92 | - **Answer**: Sessions track logged-in users (`$_SESSION['user_id']` for customers, `$_SESSION['admin_id']` for admins) to restrict access to sensitive pages. 93 | 94 | #### **5.3. How is admin access secured?** 95 | - **Answer**: Admin login checks the `role` field in the `users` table to ensure only admins can access the control panel. 96 | 97 | --- 98 | 99 | ### How to Use This Guide 100 | Follow the sections to understand: 101 | - How users and admins interact with the platform. 102 | - How the database stores and retrieves data. 103 | - The flow of actions across the website. 104 | 105 | For detailed implementation, refer to the individual PHP files for each feature. Let me know if this structure works for you! 106 | -------------------------------------------------------------------------------- /01_databaseSetup.md: -------------------------------------------------------------------------------- 1 | ### Step-by-Step Documentation for Database Setup 2 | 3 | 4 | #### Step 1: Creating the Database 5 | 1. Open **phpMyAdmin** or any database management tool. 6 | 2. Create a new database named `ecommerce`. 7 | 3. Run the following SQL commands to create the necessary tables: 8 | 9 | **Cart Table** 10 | ```sql 11 | CREATE TABLE cart ( 12 | id INT AUTO_INCREMENT PRIMARY KEY, 13 | user_id INT NOT NULL, 14 | product_id INT NOT NULL, 15 | quantity INT NOT NULL, 16 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 17 | updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 18 | ); 19 | ``` 20 | 21 | **Products Table** 22 | ```sql 23 | CREATE TABLE products ( 24 | id INT AUTO_INCREMENT PRIMARY KEY, 25 | name VARCHAR(255) NOT NULL, 26 | price DECIMAL(10, 2) NOT NULL, 27 | description TEXT, 28 | image VARCHAR(255), 29 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP 30 | ); 31 | ``` 32 | 33 | **Users Table** 34 | ```sql 35 | CREATE TABLE users ( 36 | id INT AUTO_INCREMENT PRIMARY KEY, 37 | username VARCHAR(50) NOT NULL, 38 | email VARCHAR(100) NOT NULL UNIQUE, 39 | password VARCHAR(255) NOT NULL, 40 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 41 | role ENUM('user', 'admin') DEFAULT 'user' 42 | ); 43 | ``` 44 | 45 | --- 46 | 47 | #### Step 2: Setting Up `db.php` 48 | 1. Create a folder named `includes` in your project directory. 49 | 2. Inside this folder, create a file named `db.php`. 50 | 3. Add the following code: 51 | 52 | **`db.php`** 53 | ```php 54 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 63 | } catch (PDOException $e) { 64 | echo "Connection failed: " . $e->getMessage(); 65 | } 66 | ?> 67 | ``` 68 | 69 | **Explanation**: 70 | - This script uses the **PDO (PHP Data Objects)** method for connecting to the database. 71 | - If the connection fails, an error message will be displayed. 72 | 73 | --- 74 | 75 | #### Step 3: Testing the Database Connection 76 | 1. Create a new file named `test_db.php` in the root folder. 77 | 2. Add the following code to test the connection: 78 | 79 | **`test_db.php`** 80 | ```php 81 | 90 | ``` 91 | 92 | 3. Access `http://localhost/ecommerce/test_db.php` in your browser. 93 | 4. Ensure the message **"Database connected successfully!"** is displayed. 94 | 95 | --- 96 | 97 | ### Next Steps 98 | Once the database is set up: 99 | 1. Move to the **Registration Page (`register.php`)** so users can create accounts. 100 | 2. Build the **Login Page (`login.php`)** to authenticate users. 101 | 3. Proceed to dynamic pages like `index.php`. 102 | -------------------------------------------------------------------------------- /02_registrationPageSetup.md: -------------------------------------------------------------------------------- 1 | ### Step-by-Step Documentation for the Registration Page 2 | 3 | Below is the updated and aligned documentation for creating the `register.php` page, incorporating the actual code. 4 | 5 | --- 6 | 7 | #### Step 1: Create the `register.php` File 8 | 1. Inside the `pages` folder, create a file named `register.php`. 9 | 10 | --- 11 | 12 | #### Step 2: Structure the HTML Form 13 | Add the following code to create the registration form: 14 | 15 | **HTML Form for Registration** 16 | ```php 17 | 18 | 19 | 20 | 21 | 22 | Register 23 | 82 | 83 | 84 |
85 |

Register

86 |
87 | 88 | 89 | 90 | 91 | 92 |
93 | 94 |

95 | 96 |
97 | 98 | 99 | ``` 100 | 101 | --- 102 | 103 | #### Step 3: Add PHP Logic for Registration 104 | Add this PHP script at the top of the file to handle user registration: 105 | 106 | **PHP Script for Handling Registration** 107 | ```php 108 | prepare("SELECT * FROM users WHERE email = ?"); 119 | $stmt->execute([$email]); 120 | $user = $stmt->fetch(PDO::FETCH_ASSOC); 121 | 122 | if ($user) { 123 | echo ""; 124 | } else { 125 | // Insert new user 126 | $stmt = $conn->prepare("INSERT INTO users (email, password, role) VALUES (?, ?, ?)"); 127 | $stmt->execute([$email, $password, $role]); 128 | 129 | // Log the user in after successful registration 130 | $_SESSION['user_id'] = $conn->lastInsertId(); 131 | header("Location: ../index.php"); // Redirect to the homepage 132 | exit(); 133 | } 134 | } 135 | ?> 136 | ``` 137 | 138 | --- 139 | 140 | #### Step 4: Test the Registration Page 141 | 1. Start your local server. 142 | 2. Navigate to `http://localhost/ecommerce/pages/register.php`. 143 | 3. Register using an email and password. 144 | 4. Check the `users` table in your database to ensure the new user has been added. 145 | 146 | --- 147 | 148 | ### Next Steps 149 | 1. Build the **Login Page (`login.php`)** for user authentication. 150 | 2. Implement session management to maintain user sessions. 151 | -------------------------------------------------------------------------------- /03_loginPageSetup.md: -------------------------------------------------------------------------------- 1 | ### Step-by-Step Documentation for the Login Page 2 | 3 | Below is the aligned and updated documentation for creating the `login.php` page, based on the actual code. 4 | 5 | --- 6 | 7 | #### Step 1: Create the `login.php` File 8 | 1. Inside the `pages` folder, create a file named `login.php`. 9 | 10 | --- 11 | 12 | #### Step 2: Structure the HTML Form 13 | Add the following code to create the login form: 14 | 15 | **HTML Form for Login** 16 | ```php 17 | 18 | 19 | 20 | 21 | 22 | Login 23 | 82 | 83 | 84 |
85 |

Login

86 |
87 | 88 | 89 | 90 | 91 | 92 |
93 | 94 |

95 | 96 |
97 | 98 | 99 | ``` 100 | 101 | --- 102 | 103 | #### Step 3: Add PHP Logic for Login 104 | Include this PHP script at the top of the file to handle user login requests: 105 | 106 | **PHP Script for Handling Login** 107 | ```php 108 | prepare("SELECT * FROM users WHERE email = ?"); 118 | $stmt->execute([$email]); 119 | $user = $stmt->fetch(PDO::FETCH_ASSOC); 120 | 121 | if ($user && password_verify($password, $user['password'])) { 122 | // Successful login 123 | $_SESSION['user_id'] = $user['id']; // Store user ID in session 124 | header("Location: ../index.php"); // Redirect to the main page 125 | exit(); 126 | } else { 127 | // Invalid login 128 | $error_message = "Invalid email or password."; 129 | } 130 | } 131 | ?> 132 | ``` 133 | 134 | --- 135 | 136 | #### Step 4: Test the Login Page 137 | 1. Start your local server. 138 | 2. Navigate to `http://localhost/ecommerce/pages/login.php`. 139 | 3. Use the credentials of a registered user to log in. 140 | 4. Verify successful login: 141 | - Users are redirected to the homepage. 142 | - Invalid credentials show an error message. 143 | 144 | --- 145 | 146 | ### Next Steps 147 | 1. Implement the **Logout System** (`logout.php`) to allow users to securely log out. 148 | 2. Build the **Index Page** (`index.php`). 149 | 150 | -------------------------------------------------------------------------------- /04_logoutPageSetup.md: -------------------------------------------------------------------------------- 1 | ### Step-by-Step Documentation for the Logout Page 2 | 3 | Below is the aligned and concise guide for implementing the `logout.php` page. 4 | 5 | --- 6 | 7 | #### Step 1: Create the `logout.php` File 8 | 1. Inside the `pages` folder, create a file named `logout.php`. 9 | 10 | --- 11 | 12 | #### Step 2: Add PHP Logic to Destroy the Session 13 | Add the following PHP code to handle the logout functionality: 14 | 15 | ```php 16 | 27 | ``` 28 | 29 | **Explanation**: 30 | - `session_start()`: Initializes the session. 31 | - `session_unset()`: Frees all session variables. 32 | - `session_destroy()`: Completely terminates the session. 33 | - `header('Location: login.php')`: Redirects the user to the login page after the session ends. 34 | 35 | --- 36 | 37 | #### Step 3: Add a Logout Link to Other Pages 38 | To ensure users can log out from anywhere, add a logout link to the navigation bar on user-facing pages like `index.php`: 39 | 40 | ```html 41 |
  • Logout
  • 42 | ``` 43 | 44 | --- 45 | 46 | #### Step 4: Test the Logout Page 47 | 1. Log in as a user on your site. 48 | 2. Navigate to `http://localhost/ecommerce/pages/logout.php`. 49 | 3. Confirm the following: 50 | - The session is destroyed (e.g., you are logged out). 51 | - You are redirected to the login page. 52 | - Attempting to access a protected page (e.g., `index.php`) after logging out redirects you to `login.php` if session checks are implemented. 53 | 54 | --- 55 | 56 | ### Next Steps 57 | Now that the logout functionality is implemented, the core user management system (registration, login, logout) is complete. Next, we can move to: 58 | 1. **Home Page (`index.php`)** for displaying products. 59 | -------------------------------------------------------------------------------- /05_indexPageSetup.md: -------------------------------------------------------------------------------- 1 | ### Step-by-Step Documentation for the Home Page (`index.php`) 2 | 3 | --- 4 | 5 | #### 1. Create the `index.php` File 6 | - Place this file in the root directory. 7 | - Add the following HTML structure with navigation links and a container for products: 8 | 9 | ```php 10 | 11 | 12 | 13 | 14 | 15 | Online Store 16 | 17 | 18 | 19 |
    20 |
    21 |

    Welcome to Our Store

    22 | 33 |
    34 |
    35 |
    36 |
    37 |

    Products

    38 |
    39 | 40 |
    41 |
    42 |
    43 | 46 | 47 | 48 | ``` 49 | 50 | --- 51 | 52 | #### 2. Add PHP Logic to Fetch and Display Products 53 | Insert the following PHP code at the top of the file: 54 | 55 | ```php 56 | query("SELECT * FROM products"); 62 | $products = $stmt->fetchAll(PDO::FETCH_ASSOC); 63 | ?> 64 | ``` 65 | 66 | In the `
    ` section, display the products dynamically: 67 | 68 | ```php 69 | 70 |

    No products available.

    71 | 72 | 73 |
    74 |

    75 |

    Price: $

    76 |

    77 | 78 | <?= htmlspecialchars($product['name']); ?> 79 | 80 |
    81 | 82 | 83 |
    84 |
    85 | 86 | 87 | ``` 88 | 89 | --- 90 | 91 | #### 3. Test the Home Page 92 | 1. Add sample products to the `products` table in the database. 93 | 2. Open `http://localhost/ecommerce/index.php` in your browser. 94 | 3. Verify: 95 | - Products are displayed with images, names, and prices. 96 | - Navigation links and the cart button work correctly. 97 | 98 | --- 99 | 100 | #### Next Steps 101 | 1. Implement the **Cart Page (`cart.php`)** to handle product additions. 102 | 2. Develop the **Admin Dashboard (`dashboard.php`)** for managing the store. 103 | -------------------------------------------------------------------------------- /06_cartPageSetup.md: -------------------------------------------------------------------------------- 1 | ### Brief Documentation for the Cart Page (`cart.php`) 2 | 3 | --- 4 | 5 | #### Step 1: Create `cart.php` 6 | 1. Place the file in the `pages` folder. 7 | 8 | --- 9 | 10 | #### Step 2: Display Cart Items 11 | Use the following PHP to fetch and display cart items for the logged-in user: 12 | 13 | ```php 14 | prepare("SELECT cart.id AS cart_id, products.name, products.price, cart.quantity 26 | FROM cart 27 | JOIN products ON cart.product_id = products.id 28 | WHERE cart.user_id = ?"); 29 | $stmt->execute([$user_id]); 30 | $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC); 31 | 32 | $total_cost = 0; 33 | ?> 34 | ``` 35 | 36 | In the HTML, iterate over the `$cart_items` to display them dynamically: 37 | 38 | ```php 39 | 40 | 41 | 42 | $ 43 | 44 | $ 45 | 46 |
    47 | 48 | 49 |
    50 | 51 | 52 | 53 | ``` 54 | 55 | --- 56 | 57 | #### Step 3: Handle Cart Updates 58 | Add functionality to update quantities and remove items: 59 | 60 | - **Update Quantity:** 61 | ```php 62 | if (isset($_POST['update_quantity'])) { 63 | $product_id = $_POST['product_id']; 64 | $quantity = (int)$_POST['quantity']; 65 | $stmt = $conn->prepare("UPDATE cart SET quantity = ? WHERE user_id = ? AND product_id = ?"); 66 | $stmt->execute([$quantity, $user_id, $product_id]); 67 | } 68 | ``` 69 | 70 | - **Remove Item:** 71 | ```php 72 | if (isset($_POST['remove_from_cart'])) { 73 | $product_id = $_POST['product_id']; 74 | $stmt = $conn->prepare("DELETE FROM cart WHERE user_id = ? AND product_id = ?"); 75 | $stmt->execute([$user_id, $product_id]); 76 | } 77 | ``` 78 | 79 | --- 80 | 81 | #### Step 4: Styling 82 | Add CSS to style the cart page: 83 | ```css 84 | .cart-container { 85 | max-width: 800px; 86 | margin: 20px auto; 87 | padding: 20px; 88 | background: #f9f9f9; 89 | border: 1px solid #ddd; 90 | border-radius: 8px; 91 | } 92 | ``` 93 | 94 | --- 95 | 96 | #### Step 5: Test the Cart Page 97 | 1. Log in as a user. 98 | 2. Add items to the cart from the product page. 99 | 3. Verify items are displayed correctly. 100 | 4. Check quantity updates and removal functionality. 101 | 102 | --- 103 | 104 | Next Step is Admin Dashboard Pages 105 | -------------------------------------------------------------------------------- /07_adminLoginPageSetup.md: -------------------------------------------------------------------------------- 1 | 2 | ### Step-by-Step Documentation for Admin Login Page (`admin/login.php`) 3 | 4 | --- 5 | 6 | #### Purpose 7 | The **Admin Login Page** authenticates administrators, granting access to the admin dashboard. 8 | 9 | --- 10 | 11 | #### 1. PHP Logic for Authentication 12 | The PHP code at the top handles login functionality: 13 | 14 | ```php 15 | prepare("SELECT * FROM users WHERE email = ? AND role = 'admin'"); 25 | $stmt->execute([$email]); 26 | $user = $stmt->fetch(PDO::FETCH_ASSOC); 27 | 28 | if ($user && password_verify($password, $user['password'])) { 29 | // Start admin session and redirect to dashboard 30 | $_SESSION['admin_id'] = $user['id']; 31 | header("Location: dashboard.php"); 32 | exit(); 33 | } else { 34 | echo "

    Invalid credentials or not an admin.

    "; 35 | } 36 | } 37 | ?> 38 | ``` 39 | 40 | **Key Features:** 41 | - Validates that the `email` belongs to an admin. 42 | - Verifies the password using `password_verify()`. 43 | - Starts a session for authenticated admins. 44 | - Redirects to the `dashboard.php` page upon successful login. 45 | 46 | --- 47 | 48 | #### 2. HTML Form for Login 49 | This is the front-end structure for the admin login page: 50 | 51 | ```html 52 | 53 | 54 | 55 | 56 | 57 | Admin Login 58 | 104 | 105 | 106 | 107 |
    108 |

    Admin Login

    109 |
    110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 |
    118 |
    119 | 120 | 121 | 122 | ``` 123 | 124 | **Features:** 125 | - The form uses `POST` to securely submit credentials. 126 | - Fields include `email` and `password`. 127 | 128 | --- 129 | 130 | #### 3. Test the Admin Login Page 131 | 1. Ensure the `users` table has an admin user (`role = 'admin'`). 132 | 2. Open the page at `http://localhost/ecommerce/admin/login.php`. 133 | 3. Test login with valid and invalid admin credentials. 134 | 135 | --- 136 | 137 | ### Next Step 138 | The next page is **Admin Logout (`admin/logout.php`)** to securely end admin sessions. Let’s proceed! 139 | -------------------------------------------------------------------------------- /08_adminLogoutPageSetup.md: -------------------------------------------------------------------------------- 1 | ### **1. Why There is No Admin Register Page** 2 | An **Admin Register Page** is typically excluded in secure systems for the following reasons: 3 | 4 | 1. **Security Concerns**: 5 | - Allowing anyone to register as an admin could expose the system to unauthorized access. 6 | - Admin creation is restricted to superusers or developers who have direct access to the database or backend. 7 | 8 | 2. **Controlled Access**: 9 | - Admins are added manually by authorized personnel (e.g., via a secure backend or database query). 10 | - This ensures only trusted individuals are granted administrative privileges. 11 | 12 | 3. **Simpler Role Management**: 13 | - Using a `role` field in the `users` table (`user` vs `admin`) simplifies user management without requiring separate registration flows. 14 | 15 | --- 16 | 17 | ### **2. Adding an Admin User to the Database** 18 | 19 | To add an admin user manually in the database, follow these steps: 20 | 21 | #### Using **phpMyAdmin** or SQL Console: 22 | 1. **Insert Admin User**: 23 | Run the following SQL query: 24 | ```sql 25 | INSERT INTO users (username, email, password, role, created_at) 26 | VALUES ('Admin Name', 'admin@example.com', '', 'admin', NOW()); 27 | ``` 28 | Replace: 29 | - `'Admin Name'` with the admin's name. 30 | - `'admin@example.com'` with the admin's email. 31 | - `''` with the hashed password. 32 | 33 | #### Example Query: 34 | ```sql 35 | INSERT INTO users (username, email, password, role, created_at) 36 | VALUES ('Super Admin', 'admin@ecommerce.com', '$2y$10$abcdefghijk1234567890LMNOPQRSTUVWXyz12345678', 'admin', NOW()); 37 | ``` 38 | 39 | 2. **Verify the Inserted Admin**: 40 | Query the `users` table to confirm the new admin user: 41 | ```sql 42 | SELECT * FROM users WHERE role = 'admin'; 43 | ``` 44 | 45 | --- 46 | 47 | ### **3. Next: Admin Logout Page** 48 | 49 | ### Step-by-Step Documentation for Admin Logout Page (`admin/logout.php`) 50 | 51 | --- 52 | 53 | #### Purpose 54 | The **Admin Logout Page** securely ends the admin session and redirects the user to the admin login page. 55 | 56 | --- 57 | 58 | #### Code Breakdown 59 | The code for `admin/logout.php` is simple yet effective: 60 | 61 | ```php 62 | 69 | ``` 70 | 71 | --- 72 | 73 | #### How It Works 74 | 1. **Start the Session**: 75 | - The session is initiated with `session_start()` to access and manage session variables. 76 | 77 | 2. **Unset All Variables**: 78 | - `session_unset()` removes all session variables, ensuring no leftover data persists. 79 | 80 | 3. **Destroy the Session**: 81 | - `session_destroy()` completely terminates the session, effectively logging out the admin. 82 | 83 | 4. **Redirect**: 84 | - After ending the session, the admin is redirected to `login.php` using `header("Location: login.php");`. 85 | 86 | --- 87 | 88 | #### Implementation Steps 89 | 1. Place the `logout.php` file in the `admin/` folder. 90 | 2. Add a **Logout Link** to the admin dashboard (`dashboard.php`): 91 | ```html 92 |
  • Logout
  • 93 | ``` 94 | 95 | --- 96 | 97 | #### Testing the Logout Page 98 | 1. Log in as an admin. 99 | 2. Navigate to `http://localhost/ecommerce/admin/logout.php`. 100 | 3. Confirm: 101 | - The session is ended. 102 | - You are redirected to the admin login page. 103 | - Attempting to access the dashboard (`dashboard.php`) redirects to the login page if session checks are implemented. 104 | 105 | --- 106 | 107 | Next proceed with the **Admin Dashboard (`dashboard.php`)** 108 | -------------------------------------------------------------------------------- /09_adminDashboardPageSetup.md: -------------------------------------------------------------------------------- 1 | ### Step-by-Step Documentation for Admin Dashboard (`admin/dashboard.php`) 2 | 3 | --- 4 | 5 | #### Purpose 6 | The **Admin Dashboard** serves as the central control panel for managing the e-commerce site. It provides navigation links to key admin functionalities like adding and managing products. 7 | 8 | --- 9 | 10 | #### 1. Session Authentication 11 | Ensure only authenticated admins can access the dashboard: 12 | 13 | ```php 14 | 21 | ``` 22 | 23 | **How It Works**: 24 | - `session_start()` initializes the session. 25 | - The script checks if `admin_id` is set in the session. If not, it redirects to the login page. 26 | 27 | --- 28 | 29 | #### 2. HTML Structure of the Dashboard 30 | The dashboard includes navigation links for admin operations and a logout button: 31 | 32 | ```html 33 | 34 | 35 | 36 | 37 | 38 | Admin Dashboard 39 | 87 | 88 | 89 |
    90 |

    Admin Dashboard

    91 | 96 |
    97 |
    98 |

    © Admin Dashboard

    99 |
    100 | 101 | 102 | ``` 103 | 104 | --- 105 | 106 | #### 3. Features 107 | - **Add Product**: Link to `add_product.php` for creating new product entries. 108 | - **Manage Products**: Link to `manage_products.php` for editing or deleting products. 109 | - **Logout**: Ends the admin session and redirects to the login page. 110 | 111 | --- 112 | 113 | #### 4. Testing the Dashboard 114 | 1. Log in as an admin. 115 | 2. Navigate to `http://localhost/ecommerce/admin/dashboard.php`. 116 | 3. Verify: 117 | - The session check prevents unauthorized access. 118 | - Links navigate to their respective pages. 119 | - The logout button works as expected. 120 | 121 | --- 122 | 123 | Next proceed with **Manage Products (`admin/manage_products.php`)**. 124 | -------------------------------------------------------------------------------- /10_adminManageProductsPageSetup.md: -------------------------------------------------------------------------------- 1 | ### Step-by-Step Documentation for Admin Manage Products Page (`admin/manage_products.php`) 2 | 3 | --- 4 | 5 | #### Purpose 6 | The **Manage Products Page** allows administrators to view, edit, and delete existing products in the e-commerce database. 7 | 8 | --- 9 | 10 | #### 1. Session Authentication 11 | Ensure only logged-in admins can access the page: 12 | 13 | ```php 14 | 21 | ``` 22 | 23 | --- 24 | 25 | #### 2. Fetch Products from the Database 26 | Fetch all products from the database to display in a table: 27 | 28 | ```php 29 | query("SELECT * FROM products"); 32 | $products = $stmt->fetchAll(PDO::FETCH_ASSOC); 33 | ?> 34 | ``` 35 | 36 | --- 37 | 38 | #### 3. HTML Structure 39 | Display the fetched products in a structured table: 40 | 41 | ```html 42 | 43 | 44 | 45 | 46 | 47 | Manage Products 48 | 96 | 97 | 98 | 99 |
    100 |

    Manage Products

    101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 121 | 122 | 123 |
    IDNamePriceDescriptionImageActions
    $Product Image 118 | Edit 119 | Delete 120 |
    124 | Back to Dashboard 125 |
    126 | 127 | 128 | 129 | ``` 130 | 131 | --- 132 | 133 | #### Features 134 | 1. **View Products**: 135 | - Display all products with details (ID, name, price, description, and image). 136 | 137 | 2. **Edit Products**: 138 | - Clicking the **Edit** link redirects to `edit_product.php` for modifying product details. 139 | 140 | 3. **Delete Products**: 141 | - Clicking the **Delete** link removes a product from the database after confirmation. 142 | 143 | 4. **Back to Dashboard**: 144 | - Provides a link to return to the admin dashboard. 145 | 146 | --- 147 | 148 | #### 4. Testing the Manage Products Page 149 | 1. Log in as an admin. 150 | 2. Navigate to `http://localhost/ecommerce/admin/manage_products.php`. 151 | 3. Verify: 152 | - All products are displayed correctly. 153 | - Edit and delete actions link to their respective pages. 154 | - The dashboard link works as expected. 155 | 156 | --- 157 | 158 | Next proceed with **Add Product (`admin/add_product.php`)**. 159 | -------------------------------------------------------------------------------- /11_adminAddProductPageSetup.md: -------------------------------------------------------------------------------- 1 | ### Step-by-Step Documentation for Add Product Page (`admin/add_product.php`) 2 | 3 | --- 4 | 5 | #### Purpose 6 | The **Add Product Page** allows administrators to create new product entries by filling out a form with the product’s name, price, description, and image. 7 | 8 | --- 9 | 10 | #### 1. Session Authentication 11 | Ensure only logged-in admins can access this page: 12 | 13 | ```php 14 | 21 | ``` 22 | 23 | --- 24 | 25 | #### 2. PHP Logic for Adding Products 26 | The following PHP code handles form submission and saves the new product to the database: 27 | 28 | ```php 29 | prepare("INSERT INTO products (name, price, description, image) VALUES (?, ?, ?, ?)"); 43 | $stmt->execute([$name, $price, $description, $image]); 44 | 45 | echo "Product added successfully!"; 46 | } 47 | ?> 48 | ``` 49 | 50 | --- 51 | 52 | #### 3. HTML Form for Adding Products 53 | The form captures all necessary product details: 54 | 55 | ```html 56 | 57 | 58 | 59 | 60 | 61 | Add Product 62 | 112 | 113 | 114 |
    115 |

    Add Product

    116 |
    117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 |
    131 | 134 |
    135 | 136 | 137 | ``` 138 | 139 | --- 140 | 141 | #### Features 142 | 1. **Form Inputs**: 143 | - **Product Name** (`name`) 144 | - **Price** (`price`) 145 | - **Description** (`description`) 146 | - **Image Upload** (`image`) 147 | 148 | 2. **Image Handling**: 149 | - Images are uploaded to the `images/` folder. 150 | - The filename is saved in the database. 151 | 152 | 3. **Database Insertion**: 153 | - Product details are stored in the `products` table. 154 | 155 | 4. **Feedback**: 156 | - Displays a success message after the product is added. 157 | 158 | --- 159 | 160 | #### 4. Testing the Add Product Page 161 | 1. Log in as an admin. 162 | 2. Navigate to `http://localhost/ecommerce/admin/add_product.php`. 163 | 3. Fill out the form and submit. 164 | 4. Verify: 165 | - The product is added to the `products` table. 166 | - The image file is uploaded to the `images/` folder. 167 | 168 | --- 169 | -------------------------------------------------------------------------------- /admin/add_product.php: -------------------------------------------------------------------------------- 1 | prepare("INSERT INTO products (name, price, description, image) VALUES (?, ?, ?, ?)"); 17 | $stmt->execute([$name, $price, $description, $image]); 18 | echo "Product added successfully!"; 19 | } 20 | ?> 21 | 22 | 23 | 24 | 25 | 26 | Add Product 27 | 111 | 112 | 113 |
    114 |

    Add Product

    115 |
    116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 |
    130 | 131 | 132 |
    133 | 134 | 135 | 138 |
    139 | 140 | 141 | -------------------------------------------------------------------------------- /admin/dashboard.php: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | 11 | 12 | 13 | Admin Dashboard 14 | 73 | 74 | 75 |
    76 |

    Admin Dashboard

    77 | 82 |
    83 | 84 |
    85 |

    © Admin Dashboard

    86 |
    87 | 88 | 89 | -------------------------------------------------------------------------------- /admin/login.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT * FROM users WHERE email = ? AND role = 'admin'"); 10 | $stmt->execute([$email]); 11 | $user = $stmt->fetch(PDO::FETCH_ASSOC); 12 | 13 | if ($user && password_verify($password, $user['password'])) { 14 | $_SESSION['admin_id'] = $user['id']; 15 | header("Location: dashboard.php"); 16 | exit(); 17 | } else { 18 | echo "

    Invalid credentials or not an admin.

    "; 19 | } 20 | } 21 | ?> 22 | 23 | 24 | 25 | 26 | 27 | Admin Login 28 | 87 | 88 | 89 | 90 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /admin/logout.php: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /admin/manage_products.php: -------------------------------------------------------------------------------- 1 | query("SELECT * FROM products"); 10 | $products = $stmt->fetchAll(PDO::FETCH_ASSOC); 11 | ?> 12 | 13 | 14 | 15 | 16 | 17 | Manage Products 18 | 91 | 92 | 93 | 94 |
    95 |

    Manage Products

    96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 118 | 119 | 120 | 121 |
    IDNamePriceDescriptionImageActions
    $Product Image 115 | Edit 116 | Delete 117 |
    122 | 123 | Back to Dashboard 124 |
    125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /css/style.css: -------------------------------------------------------------------------------- 1 | /* General Body and Layout */ 2 | body { 3 | font-family: 'Arial', sans-serif; 4 | margin: 0; 5 | padding: 0; 6 | background-color: #f7f7f7; 7 | color: #333; 8 | } 9 | 10 | /* Header */ 11 | header { 12 | background-color: #2c3e50; 13 | color: white; 14 | padding: 20px; 15 | text-align: center; 16 | position: relative; 17 | } 18 | 19 | .header-container { 20 | display: flex; 21 | justify-content: space-between; 22 | align-items: center; 23 | padding: 0 20px; 24 | width: 100%; /* Ensures full width */ 25 | } 26 | 27 | header h1 { 28 | margin: 0; 29 | font-size: 2em; 30 | } 31 | 32 | /* Updated navigation styling to align buttons in one row */ 33 | nav { 34 | display: flex; 35 | align-items: center; /* Align buttons vertically centered */ 36 | } 37 | 38 | nav a, .logout-button { 39 | color: white; 40 | text-decoration: none; 41 | margin: 0 15px; 42 | font-size: 1em; 43 | text-transform: uppercase; 44 | display: inline-block; 45 | } 46 | 47 | nav a:hover, .logout-button:hover { 48 | text-decoration: underline; 49 | } 50 | 51 | .logout-button { 52 | background-color: #ff5733; 53 | color: white; 54 | border: none; 55 | padding: 8px 12px; 56 | border-radius: 4px; 57 | cursor: pointer; 58 | transition: background-color 0.3s; 59 | } 60 | 61 | .logout-button:hover { 62 | background-color: #e84e2f; 63 | } 64 | 65 | /* Cart Link - Fixed beside Register button */ 66 | .cart-link { 67 | color: white; 68 | display: flex; 69 | align-items: center; 70 | text-decoration: none; 71 | font-size: 1.1em; 72 | } 73 | 74 | .cart-link:hover { 75 | color: #16a085; 76 | } 77 | 78 | .cart-icon { 79 | width: 25px; 80 | height: 25px; 81 | margin-right: 8px; 82 | } 83 | 84 | /* Main Container */ 85 | .main-container { 86 | padding: 20px; 87 | display: flex; 88 | justify-content: center; 89 | flex-wrap: wrap; /* Allows responsiveness */ 90 | } 91 | 92 | /* Product Listing */ 93 | .product-list { 94 | display: flex; 95 | flex-wrap: wrap; 96 | justify-content: space-between; 97 | gap: 20px; 98 | width: 80%; /* Take up most of the screen */ 99 | } 100 | 101 | .product { 102 | background-color: #fff; 103 | padding: 15px; 104 | box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); 105 | border-radius: 8px; 106 | text-align: center; 107 | width: 23%; /* Set width for 4 products per row */ 108 | transition: transform 0.3s ease-in-out; 109 | margin-bottom: 20px; 110 | } 111 | 112 | .product:hover { 113 | transform: translateY(-10px); 114 | } 115 | 116 | .product h3 { 117 | margin-bottom: 10px; 118 | font-size: 1.3em; 119 | color: #333; 120 | } 121 | 122 | .product p { 123 | font-size: 1em; 124 | color: #777; 125 | margin-bottom: 10px; 126 | } 127 | 128 | .product-image { 129 | width: 100%; 130 | height: 150px; 131 | object-fit: cover; 132 | border-radius: 8px; 133 | margin: 10px 0; 134 | } 135 | 136 | .add-to-cart-button { 137 | background-color: #2ecc71; 138 | color: white; 139 | padding: 12px 25px; 140 | border: none; 141 | border-radius: 6px; 142 | font-size: 1.2em; 143 | cursor: pointer; 144 | transition: background-color 0.3s, transform 0.3s; 145 | } 146 | 147 | .add-to-cart-button:hover { 148 | background-color: #27ae60; 149 | transform: scale(1.05); 150 | } 151 | 152 | /* Footer */ 153 | footer { 154 | background-color: #2c3e50; 155 | color: white; 156 | text-align: center; 157 | padding: 20px 0; 158 | margin-top: 40px; 159 | } 160 | 161 | footer p { 162 | margin: 0; 163 | font-size: 1.1em; 164 | } 165 | -------------------------------------------------------------------------------- /images/cart-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BJSwaroop/ecommerce_codewithswaroop/ec3055f7086f65ea6b638b14a2ef91e1b499b9f9/images/cart-icon.png -------------------------------------------------------------------------------- /images/product1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BJSwaroop/ecommerce_codewithswaroop/ec3055f7086f65ea6b638b14a2ef91e1b499b9f9/images/product1.jpg -------------------------------------------------------------------------------- /images/product2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BJSwaroop/ecommerce_codewithswaroop/ec3055f7086f65ea6b638b14a2ef91e1b499b9f9/images/product2.jpg -------------------------------------------------------------------------------- /images/product3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BJSwaroop/ecommerce_codewithswaroop/ec3055f7086f65ea6b638b14a2ef91e1b499b9f9/images/product3.jpg -------------------------------------------------------------------------------- /images/product4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BJSwaroop/ecommerce_codewithswaroop/ec3055f7086f65ea6b638b14a2ef91e1b499b9f9/images/product4.jpg -------------------------------------------------------------------------------- /images/product5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BJSwaroop/ecommerce_codewithswaroop/ec3055f7086f65ea6b638b14a2ef91e1b499b9f9/images/product5.png -------------------------------------------------------------------------------- /includes/db.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 10 | } catch (PDOException $e) { 11 | echo "Connection failed: " . $e->getMessage(); 12 | } 13 | ?> 14 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | query("SELECT * FROM products"); 23 | $products = $stmt->fetchAll(PDO::FETCH_ASSOC); 24 | ?> 25 | 26 | 27 | 28 | 29 | 30 | Online Store 31 | 32 | 33 | 34 |
    35 |
    36 |

    Welcome to Our Store

    37 | 49 |
    50 |
    51 |
    52 |
    53 |

    Products

    54 |
    55 | 56 |

    No products available.

    57 | 58 | 59 |
    60 |

    61 |

    Price: $

    62 |

    63 | 64 | <?= htmlspecialchars($product['name']); ?> 65 | 66 |
    67 | 68 | 69 |
    70 |
    71 | 72 | 73 |
    74 |
    75 |
    76 |
    77 |

    © Online Store. All rights reserved.

    78 |
    79 | 80 | 81 | -------------------------------------------------------------------------------- /pages/cart.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT * FROM cart WHERE user_id = ? AND product_id = ?"); 19 | $stmt->execute([$user_id, $product_id]); 20 | $cart_item = $stmt->fetch(PDO::FETCH_ASSOC); 21 | 22 | if ($cart_item) { 23 | // Update quantity if the product is already in the cart 24 | $new_quantity = $cart_item['quantity'] + $quantity; 25 | $stmt = $conn->prepare("UPDATE cart SET quantity = ? WHERE user_id = ? AND product_id = ?"); 26 | $stmt->execute([$new_quantity, $user_id, $product_id]); 27 | } else { 28 | // Add new product to the cart 29 | $stmt = $conn->prepare("INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)"); 30 | $stmt->execute([$user_id, $product_id, $quantity]); 31 | } 32 | } 33 | 34 | // Handle Product Removal from Cart 35 | if (isset($_POST['remove_from_cart'])) { 36 | $product_id = $_POST['product_id']; 37 | $stmt = $conn->prepare("DELETE FROM cart WHERE user_id = ? AND product_id = ?"); 38 | $stmt->execute([$user_id, $product_id]); 39 | } 40 | 41 | // Handle Quantity Update 42 | if (isset($_POST['update_quantity'])) { 43 | $product_id = $_POST['product_id']; 44 | $quantity = (int)$_POST['quantity']; 45 | 46 | // Update the quantity in the cart 47 | $stmt = $conn->prepare("UPDATE cart SET quantity = ? WHERE user_id = ? AND product_id = ?"); 48 | $stmt->execute([$quantity, $user_id, $product_id]); 49 | } 50 | 51 | // Fetch the user's cart items 52 | $stmt = $conn->prepare("SELECT * FROM cart WHERE user_id = ?"); 53 | $stmt->execute([$user_id]); 54 | $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC); 55 | 56 | $total_cost = 0; // Initialize total cost variable 57 | ?> 58 | 59 | 60 | 61 | 62 | 63 | Your Cart 64 | 170 | 171 | 172 |
    173 |

    Your Cart

    174 | Your cart is empty.

    "; 177 | } else { 178 | // Fetch product details for each cart item 179 | $product_ids = array_column($cart_items, 'product_id'); 180 | $placeholders = implode(',', array_fill(0, count($product_ids), '?')); 181 | $stmt = $conn->prepare("SELECT * FROM products WHERE id IN ($placeholders)"); 182 | $stmt->execute($product_ids); 183 | $products = $stmt->fetchAll(PDO::FETCH_ASSOC); 184 | 185 | foreach ($products as $product) { 186 | $quantity = 0; 187 | foreach ($cart_items as $cart_item) { 188 | if ($cart_item['product_id'] == $product['id']) { 189 | $quantity = $cart_item['quantity']; 190 | break; 191 | } 192 | } 193 | $total_cost += $product['price'] * $quantity; // Add product price * quantity to total cost 194 | 195 | echo "
    196 | {$product[ 197 |
    198 |
    {$product['name']}
    199 |
    \${$product['price']} x $quantity
    200 |
    201 |
    202 |
    203 | 204 | 205 | 206 |
    207 |
    208 | 209 | 210 |
    211 |
    212 |
    "; 213 | } 214 | } 215 | ?> 216 | 217 |
    218 | Total: $ 219 |
    220 | 221 |
    222 | Back to Shop 223 | Proceed to Checkout 224 |
    225 |
    226 | 227 | 228 | -------------------------------------------------------------------------------- /pages/login.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT * FROM users WHERE email = ?"); 11 | $stmt->execute([$email]); 12 | $user = $stmt->fetch(PDO::FETCH_ASSOC); 13 | 14 | if ($user && password_verify($password, $user['password'])) { 15 | // Successful login 16 | $_SESSION['user_id'] = $user['id']; // Store user ID in session 17 | header("Location: ../index.php"); // Redirect to the main page 18 | exit(); 19 | } else { 20 | // Invalid login 21 | $error_message = "Invalid email or password."; 22 | } 23 | } 24 | ?> 25 | 26 | 27 | 28 | 29 | 30 | Login 31 | 90 | 91 | 92 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /pages/logout.php: -------------------------------------------------------------------------------- 1 | 7 | -------------------------------------------------------------------------------- /pages/register.php: -------------------------------------------------------------------------------- 1 | prepare("SELECT * FROM users WHERE email = ?"); 12 | $stmt->execute([$email]); 13 | $user = $stmt->fetch(PDO::FETCH_ASSOC); 14 | 15 | if ($user) { 16 | echo "Email is already registered!"; 17 | } else { 18 | // Insert new user 19 | $stmt = $conn->prepare("INSERT INTO users (email, password, role) VALUES (?, ?, ?)"); 20 | $stmt->execute([$email, $password, $role]); 21 | 22 | // After registration, log the user in and redirect to the main page 23 | $_SESSION['user_id'] = $conn->lastInsertId(); // Store the user ID in session 24 | header("Location: ../index.php"); // Redirect to the main page 25 | exit(); 26 | } 27 | } 28 | ?> 29 | 30 | 31 | 32 | 33 | 34 | Register 35 | 94 | 95 | 96 |
    97 |

    Register

    98 |
    99 | 100 | 101 | 102 | 103 | 104 |
    105 | 106 |

    107 | 108 |
    109 | 110 | 111 | --------------------------------------------------------------------------------