Spaces:
Running
Running
| ```php | |
| require_once 'database_connection.php'; | |
| session_start(); | |
| if (!isset($_SESSION['admin_logged_in'])) { | |
| header('Location: admin_login.php'); | |
| exit; | |
| } | |
| $message = ''; | |
| $error = ''; | |
| // Handle form submission | |
| if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
| $name = $_POST['name'] ?? ''; | |
| $description = $_POST['description'] ?? ''; | |
| $affiliate_url = $_POST['affiliate_url'] ?? ''; | |
| // Handle file upload | |
| if (isset($_FILES['product_image']) && $_FILES['product_image']['error'] === UPLOAD_ERR_OK) { | |
| $uploadDir = 'uploads/'; | |
| if (!file_exists($uploadDir)) { | |
| mkdir($uploadDir, 0755, true); | |
| } | |
| $fileName = uniqid() . '_' . basename($_FILES['product_image']['name']); | |
| $targetPath = $uploadDir . $fileName; | |
| if (move_uploaded_file($_FILES['product_image']['tmp_name'], $targetPath)) { | |
| // Insert product into database | |
| $stmt = $pdo->prepare("INSERT INTO products (name, description, image_path, affiliate_url) | |
| VALUES (?, ?, ?, ?)"); | |
| $stmt->execute([$name, $description, $targetPath, $affiliate_url]); | |
| $message = 'Product added successfully!'; | |
| } else { | |
| $error = 'Failed to upload image'; | |
| } | |
| } else { | |
| $error = 'Please select an image'; | |
| } | |
| } | |
| // Get all products for listing | |
| $products = $pdo->query("SELECT * FROM products ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC); | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>Admin Dashboard</title> | |
| <style> | |
| body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; } | |
| .message { background: #d4edda; color: #155724; padding: 10px; margin-bottom: 20px; border-radius: 5px; } | |
| .error { background: #f8d7da; color: #721c24; padding: 10px; margin-bottom: 20px; border-radius: 5px; } | |
| .form-group { margin-bottom: 15px; } | |
| label { display: block; margin-bottom: 5px; } | |
| input, textarea { width: 100%; padding: 8px; box-sizing: border-box; } | |
| button { background: #007bff; color: white; border: none; padding: 10px 15px; cursor: pointer; } | |
| .products { margin-top: 30px; } | |
| .product { border: 1px solid #ddd; padding: 15px; margin-bottom: 15px; border-radius: 5px; } | |
| .logout { float: right; } | |
| </style> | |
| </head> | |
| <body> | |
| <h1>Admin Dashboard <a href="admin_logout.php" class="logout">Logout</a></h1> | |
| <?php if ($message): ?> | |
| <div class="message"><?= htmlspecialchars($message) ?></div> | |
| <?php endif; ?> | |
| <?php if ($error): ?> | |
| <div class="error"><?= htmlspecialchars($error) ?></div> | |
| <?php endif; ?> | |
| <h2>Add New Product</h2> | |
| <form method="POST" enctype="multipart/form-data"> | |
| <div class="form-group"> | |
| <label for="name">Product Name:</label> | |
| <input type="text" id="name" name="name" required> | |
| </div> | |
| <div class="form-group"> | |
| <label for="description">Description:</label> | |
| <textarea id="description" name="description" rows="3"></textarea> | |
| </div> | |
| <div class="form-group"> | |
| <label for="affiliate_url">Amazon Affiliate URL:</label> | |
| <input type="url" id="affiliate_url" name="affiliate_url" required> | |
| </div> | |
| <div class="form-group"> | |
| <label for="product_image">Product Image:</label> | |
| <input type="file" id="product_image" name="product_image" required accept="image/*"> | |
| </div> | |
| <button type="submit">Add Product</button> | |
| </form> | |
| <div class="products"> | |
| <h2>Current Products</h2> | |
| <?php foreach ($products as $product): ?> | |
| <div class="product"> | |
| <img src="<?= htmlspecialchars($product['image_path']) ?>" alt="<?= htmlspecialchars($product['name']) ?>" style="max-width: 200px;"> | |
| <h3><?= htmlspecialchars($product['name']) ?></h3> | |
| <p><?= htmlspecialchars($product['description']) ?></p> | |
| <p><a href="<?= htmlspecialchars($product['affiliate_url']) ?>" target="_blank">Affiliate Link</a></p> | |
| </div> | |
| <?php endforeach; ?> | |
| </div> | |
| </body> | |
| </html> | |
| ``` |