Spaces:
Running
Running
File size: 4,334 Bytes
3b11c5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
```php
<?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>
``` |