Taper5749 commited on
Commit
3b11c5d
·
verified ·
1 Parent(s): 81d3a1f

Custom E-Commerce Showcase Prompt

Browse files

Goal: Generate the complete PHP code and MySQL database structure for a simple product showcase website.
Key Requirements:
* Technology: The entire application must be built using PHP for the backend logic and front-end rendering.
* Database: A MySQL database is required for storing product data and user credentials. Please provide the SQL schema to create the necessary tables.
* Core Functionality (Product Showcase):
* A simple, clean public-facing page that displays all products.
* Each product must show a picture (stored locally, with the path saved in the database) and a button/link (which uses the stored Amazon affiliate URL).
* Admin Area:
* A secure admin login page (e.g., /admin.php).
* The admin must be able to log in using credentials stored in the database.
* Once logged in, the admin must have a simple form to upload a product picture and paste the corresponding Amazon affiliate link.
* The system should save the image file and the affiliate link to the database.
* Visitor Counter Feature:
* A separate table in the database must be used to track site visits.
* The public-facing page must display two real-time statistics at the top:
* Total Visitor Count (The total number of unique visits since the site launched).
* Live Visitor Count (The number of users currently active on the site within the last 5 minutes).
Please provide:
* The necessary MySQL table creation SQL statements.
* The complete PHP code for all required files (e.g., index.php, admin_login.php, admin_dashboard.php, database_connection.php).
* Clear instructions on how to set up the admin credentials.

Files changed (6) hide show
  1. admin_dashboard.php +111 -0
  2. admin_login.php +67 -0
  3. admin_logout.php +29 -0
  4. database.sql +39 -0
  5. database_connection.php +15 -0
  6. index.php +68 -0
admin_dashboard.php ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```php
2
+ <?php
3
+ require_once 'database_connection.php';
4
+ session_start();
5
+
6
+ if (!isset($_SESSION['admin_logged_in'])) {
7
+ header('Location: admin_login.php');
8
+ exit;
9
+ }
10
+
11
+ $message = '';
12
+ $error = '';
13
+
14
+ // Handle form submission
15
+ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
16
+ $name = $_POST['name'] ?? '';
17
+ $description = $_POST['description'] ?? '';
18
+ $affiliate_url = $_POST['affiliate_url'] ?? '';
19
+
20
+ // Handle file upload
21
+ if (isset($_FILES['product_image']) && $_FILES['product_image']['error'] === UPLOAD_ERR_OK) {
22
+ $uploadDir = 'uploads/';
23
+ if (!file_exists($uploadDir)) {
24
+ mkdir($uploadDir, 0755, true);
25
+ }
26
+
27
+ $fileName = uniqid() . '_' . basename($_FILES['product_image']['name']);
28
+ $targetPath = $uploadDir . $fileName;
29
+
30
+ if (move_uploaded_file($_FILES['product_image']['tmp_name'], $targetPath)) {
31
+ // Insert product into database
32
+ $stmt = $pdo->prepare("INSERT INTO products (name, description, image_path, affiliate_url)
33
+ VALUES (?, ?, ?, ?)");
34
+ $stmt->execute([$name, $description, $targetPath, $affiliate_url]);
35
+ $message = 'Product added successfully!';
36
+ } else {
37
+ $error = 'Failed to upload image';
38
+ }
39
+ } else {
40
+ $error = 'Please select an image';
41
+ }
42
+ }
43
+
44
+ // Get all products for listing
45
+ $products = $pdo->query("SELECT * FROM products ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
46
+ ?>
47
+ <!DOCTYPE html>
48
+ <html lang="en">
49
+ <head>
50
+ <meta charset="UTF-8">
51
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
52
+ <title>Admin Dashboard</title>
53
+ <style>
54
+ body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
55
+ .message { background: #d4edda; color: #155724; padding: 10px; margin-bottom: 20px; border-radius: 5px; }
56
+ .error { background: #f8d7da; color: #721c24; padding: 10px; margin-bottom: 20px; border-radius: 5px; }
57
+ .form-group { margin-bottom: 15px; }
58
+ label { display: block; margin-bottom: 5px; }
59
+ input, textarea { width: 100%; padding: 8px; box-sizing: border-box; }
60
+ button { background: #007bff; color: white; border: none; padding: 10px 15px; cursor: pointer; }
61
+ .products { margin-top: 30px; }
62
+ .product { border: 1px solid #ddd; padding: 15px; margin-bottom: 15px; border-radius: 5px; }
63
+ .logout { float: right; }
64
+ </style>
65
+ </head>
66
+ <body>
67
+ <h1>Admin Dashboard <a href="admin_logout.php" class="logout">Logout</a></h1>
68
+
69
+ <?php if ($message): ?>
70
+ <div class="message"><?= htmlspecialchars($message) ?></div>
71
+ <?php endif; ?>
72
+
73
+ <?php if ($error): ?>
74
+ <div class="error"><?= htmlspecialchars($error) ?></div>
75
+ <?php endif; ?>
76
+
77
+ <h2>Add New Product</h2>
78
+ <form method="POST" enctype="multipart/form-data">
79
+ <div class="form-group">
80
+ <label for="name">Product Name:</label>
81
+ <input type="text" id="name" name="name" required>
82
+ </div>
83
+ <div class="form-group">
84
+ <label for="description">Description:</label>
85
+ <textarea id="description" name="description" rows="3"></textarea>
86
+ </div>
87
+ <div class="form-group">
88
+ <label for="affiliate_url">Amazon Affiliate URL:</label>
89
+ <input type="url" id="affiliate_url" name="affiliate_url" required>
90
+ </div>
91
+ <div class="form-group">
92
+ <label for="product_image">Product Image:</label>
93
+ <input type="file" id="product_image" name="product_image" required accept="image/*">
94
+ </div>
95
+ <button type="submit">Add Product</button>
96
+ </form>
97
+
98
+ <div class="products">
99
+ <h2>Current Products</h2>
100
+ <?php foreach ($products as $product): ?>
101
+ <div class="product">
102
+ <img src="<?= htmlspecialchars($product['image_path']) ?>" alt="<?= htmlspecialchars($product['name']) ?>" style="max-width: 200px;">
103
+ <h3><?= htmlspecialchars($product['name']) ?></h3>
104
+ <p><?= htmlspecialchars($product['description']) ?></p>
105
+ <p><a href="<?= htmlspecialchars($product['affiliate_url']) ?>" target="_blank">Affiliate Link</a></p>
106
+ </div>
107
+ <?php endforeach; ?>
108
+ </div>
109
+ </body>
110
+ </html>
111
+ ```
admin_login.php ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```php
2
+ <?php
3
+ require_once 'database_connection.php';
4
+ session_start();
5
+
6
+ if (isset($_SESSION['admin_logged_in'])) {
7
+ header('Location: admin_dashboard.php');
8
+ exit;
9
+ }
10
+
11
+ $error = '';
12
+ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
13
+ $username = $_POST['username'] ?? '';
14
+ $password = $_POST['password'] ?? '';
15
+
16
+ $stmt = $pdo->prepare("SELECT * FROM admins WHERE username = ?");
17
+ $stmt->execute([$username]);
18
+ $admin = $stmt->fetch();
19
+
20
+ if ($admin && password_verify($password, $admin['password'])) {
21
+ $_SESSION['admin_logged_in'] = true;
22
+ header('Location: admin_dashboard.php');
23
+ exit;
24
+ } else {
25
+ $error = 'Invalid username or password';
26
+ }
27
+ }
28
+ ?>
29
+ <!DOCTYPE html>
30
+ <html lang="en">
31
+ <head>
32
+ <meta charset="UTF-8">
33
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
34
+ <title>Admin Login</title>
35
+ <style>
36
+ body { font-family: Arial, sans-serif; max-width: 400px; margin: 50px auto; padding: 20px; }
37
+ .login-form { border: 1px solid #ddd; padding: 20px; border-radius: 5px; }
38
+ .form-group { margin-bottom: 15px; }
39
+ label { display: block; margin-bottom: 5px; }
40
+ input { width: 100%; padding: 8px; box-sizing: border-box; }
41
+ button { background: #007bff; color: white; border: none; padding: 10px 15px; cursor: pointer; }
42
+ .error { color: red; margin-bottom: 15px; }
43
+ </style>
44
+ </head>
45
+ <body>
46
+ <h1>Admin Login</h1>
47
+
48
+ <?php if ($error): ?>
49
+ <div class="error"><?= htmlspecialchars($error) ?></div>
50
+ <?php endif; ?>
51
+
52
+ <div class="login-form">
53
+ <form method="POST">
54
+ <div class="form-group">
55
+ <label for="username">Username:</label>
56
+ <input type="text" id="username" name="username" required>
57
+ </div>
58
+ <div class="form-group">
59
+ <label for="password">Password:</label>
60
+ <input type="password" id="password" name="password" required>
61
+ </div>
62
+ <button type="submit">Login</button>
63
+ </form>
64
+ </div>
65
+ </body>
66
+ </html>
67
+ ```
admin_logout.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```php
2
+ <?php
3
+ session_start();
4
+ session_destroy();
5
+ header('Location: admin_login.php');
6
+ exit;
7
+ ```
8
+
9
+ Setup Instructions:
10
+ 1. Create the database by running the SQL in database.sql
11
+ 2. Change the default admin password (currently "password") by running:
12
+ ```sql
13
+ UPDATE admins SET password = '$2y$10$yourhashedpassword' WHERE username = 'admin';
14
+ ```
15
+ (Generate a new hash using `password_hash('yournewpassword', PASSWORD_BCRYPT)` in PHP)
16
+ 3. Create an "uploads" directory in your project root and make it writable
17
+ 4. Configure database credentials in database_connection.php
18
+ 5. The system is now ready to use:
19
+ - Public page: index.php
20
+ - Admin login: admin_login.php (username: admin, password: password - change this!)
21
+ - Admin dashboard: admin_dashboard.php (accessible after login)
22
+ - Logout: admin_logout.php
23
+
24
+ Key Features:
25
+ - Secure admin authentication with password hashing
26
+ - Visitor tracking with total and live visitor counts
27
+ - Product management with image uploads
28
+ - Clean responsive design
29
+ - Protection against common security issues (SQL injection, XSS)
database.sql ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```sql
2
+ -- Database schema for e-commerce showcase
3
+ CREATE DATABASE IF NOT EXISTS ecommerce_showcase;
4
+ USE ecommerce_showcase;
5
+
6
+ -- Admin users table
7
+ CREATE TABLE IF NOT EXISTS admins (
8
+ id INT AUTO_INCREMENT PRIMARY KEY,
9
+ username VARCHAR(50) NOT NULL UNIQUE,
10
+ password VARCHAR(255) NOT NULL,
11
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
12
+ );
13
+
14
+ -- Products table
15
+ CREATE TABLE IF NOT EXISTS products (
16
+ id INT AUTO_INCREMENT PRIMARY KEY,
17
+ name VARCHAR(100) NOT NULL,
18
+ description TEXT,
19
+ image_path VARCHAR(255) NOT NULL,
20
+ affiliate_url VARCHAR(255) NOT NULL,
21
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
22
+ );
23
+
24
+ -- Visitor tracking tables
25
+ CREATE TABLE IF NOT EXISTS visitor_counts (
26
+ id INT AUTO_INCREMENT PRIMARY KEY,
27
+ total_visits INT DEFAULT 0
28
+ );
29
+
30
+ CREATE TABLE IF NOT EXISTS live_visitors (
31
+ id INT AUTO_INCREMENT PRIMARY KEY,
32
+ session_id VARCHAR(255) NOT NULL UNIQUE,
33
+ last_activity TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
34
+ );
35
+
36
+ -- Insert initial admin (change password after setup)
37
+ INSERT INTO admins (username, password) VALUES ('admin', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi'); -- password is "password"
38
+ INSERT INTO visitor_counts (total_visits) VALUES (0);
39
+ ```
database_connection.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```php
2
+ <?php
3
+ $host = 'localhost';
4
+ $dbname = 'ecommerce_showcase';
5
+ $username = 'root';
6
+ $password = '';
7
+
8
+ try {
9
+ $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
10
+ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
11
+ } catch (PDOException $e) {
12
+ die("Database connection failed: " . $e->getMessage());
13
+ }
14
+ ?>
15
+ ```
index.php ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ```php
2
+ <?php
3
+ require_once 'database_connection.php';
4
+ session_start();
5
+
6
+ // Visitor tracking
7
+ if (!isset($_SESSION['visitor_id'])) {
8
+ $_SESSION['visitor_id'] = session_id();
9
+
10
+ // Update total visitor count
11
+ $stmt = $pdo->prepare("UPDATE visitor_counts SET total_visits = total_visits + 1");
12
+ $stmt->execute();
13
+ }
14
+
15
+ // Update or insert live visitor
16
+ $stmt = $pdo->prepare("INSERT INTO live_visitors (session_id) VALUES (:session_id)
17
+ ON DUPLICATE KEY UPDATE last_activity = CURRENT_TIMESTAMP");
18
+ $stmt->execute([':session_id' => $_SESSION['visitor_id']]);
19
+
20
+ // Clean up old live visitors (older than 5 minutes)
21
+ $stmt = $pdo->prepare("DELETE FROM live_visitors WHERE last_activity < DATE_SUB(NOW(), INTERVAL 5 MINUTE)");
22
+ $stmt->execute();
23
+
24
+ // Get visitor stats
25
+ $totalVisits = $pdo->query("SELECT total_visits FROM visitor_counts")->fetchColumn();
26
+ $liveVisitors = $pdo->query("SELECT COUNT(*) FROM live_visitors")->fetchColumn();
27
+
28
+ // Get all products
29
+ $products = $pdo->query("SELECT * FROM products ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
30
+ ?>
31
+ <!DOCTYPE html>
32
+ <html lang="en">
33
+ <head>
34
+ <meta charset="UTF-8">
35
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
36
+ <title>Product Showcase</title>
37
+ <style>
38
+ body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
39
+ .stats { background: #f5f5f5; padding: 10px; margin-bottom: 20px; border-radius: 5px; }
40
+ .products { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 20px; }
41
+ .product { border: 1px solid #ddd; padding: 15px; border-radius: 5px; }
42
+ .product img { max-width: 100%; height: auto; }
43
+ .product a { display: inline-block; margin-top: 10px; background: #007bff; color: white; padding: 8px 15px; text-decoration: none; border-radius: 4px; }
44
+ .admin-link { float: right; }
45
+ </style>
46
+ </head>
47
+ <body>
48
+ <div class="stats">
49
+ <span>Total Visitors: <?= $totalVisits ?></span> |
50
+ <span>Live Visitors: <?= $liveVisitors ?></span>
51
+ <a href="admin_login.php" class="admin-link">Admin Login</a>
52
+ </div>
53
+
54
+ <h1>Our Products</h1>
55
+
56
+ <div class="products">
57
+ <?php foreach ($products as $product): ?>
58
+ <div class="product">
59
+ <img src="<?= htmlspecialchars($product['image_path']) ?>" alt="<?= htmlspecialchars($product['name']) ?>">
60
+ <h3><?= htmlspecialchars($product['name']) ?></h3>
61
+ <p><?= htmlspecialchars($product['description']) ?></p>
62
+ <a href="<?= htmlspecialchars($product['affiliate_url']) ?>" target="_blank">View on Amazon</a>
63
+ </div>
64
+ <?php endforeach; ?>
65
+ </div>
66
+ </body>
67
+ </html>
68
+ ```