163 lines
7.8 KiB
PHP
163 lines
7.8 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
// Create table if it doesn't exist
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS `affiliate_products` (
|
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
|
`product_name` VARCHAR(255) NOT NULL,
|
|
`affiliate_link` VARCHAR(2048) NOT NULL,
|
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
// Handle form submission for adding a new product
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_product'])) {
|
|
$product_name = trim($_POST['product_name']);
|
|
$affiliate_link = trim($_POST['affiliate_link']);
|
|
|
|
if (!empty($product_name) && !empty($affiliate_link) && filter_var($affiliate_link, FILTER_VALIDATE_URL)) {
|
|
$stmt = $pdo->prepare("INSERT INTO affiliate_products (product_name, affiliate_link) VALUES (?, ?)");
|
|
$stmt->execute([$product_name, $affiliate_link]);
|
|
header("Location: products.php?status=success");
|
|
exit;
|
|
} else {
|
|
$error = "Invalid input. Please provide a valid product name and URL.";
|
|
}
|
|
}
|
|
|
|
// Handle deletion
|
|
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
|
|
$id = filter_var($_GET['id'], FILTER_VALIDATE_INT);
|
|
if ($id) {
|
|
$stmt = $pdo->prepare("DELETE FROM affiliate_products WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
header("Location: products.php?status=deleted");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Fetch all products
|
|
$products = $pdo->query("SELECT * FROM affiliate_products ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
$error = "Database error: " . $e->getMessage();
|
|
$products = [];
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Affiliate Product Management</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body data-bs-theme="dark">
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="index.php">AI Reel Creator</a>
|
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
|
<span class="navbar-toggler-icon"></span>
|
|
</button>
|
|
<div class="collapse navbar-collapse" id="navbarNav">
|
|
<ul class="navbar-nav ms-auto">
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="index.php#features">Features</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link" href="index.php#create">Create</a>
|
|
</li>
|
|
<li class="nav-item">
|
|
<a class="nav-link active" href="products.php">Products</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container mt-5 pt-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-10">
|
|
<h1 class="display-4 text-center mb-5">Affiliate Product Management</h1>
|
|
|
|
<?php if (isset($error)): ?>
|
|
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
|
|
<?php endif; ?>
|
|
<?php if (isset($_GET['status']) && $_GET['status'] === 'success'): ?>
|
|
<div class="alert alert-success">Product added successfully!</div>
|
|
<?php endif; ?>
|
|
<?php if (isset($_GET['status']) && $_GET['status'] === 'deleted'): ?>
|
|
<div class="alert alert-info">Product deleted.</div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Add Product Form -->
|
|
<div class="card bg-dark-surface mb-5">
|
|
<div class="card-body">
|
|
<h2 class="card-title h4">Add New Product</h2>
|
|
<form action="products.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="product_name" class="form-label">Product Name</label>
|
|
<input type="text" class="form-control" id="product_name" name="product_name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="affiliate_link" class="form-label">Affiliate Link (URL)</label>
|
|
<input type="url" class="form-control" id="affiliate_link" name="affiliate_link" placeholder="https://..." required>
|
|
</div>
|
|
<button type="submit" name="add_product" class="btn btn-primary-custom">Add Product</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Product List -->
|
|
<div class="card bg-dark-surface">
|
|
<div class="card-body">
|
|
<h2 class="card-title h4">Existing Products</h2>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Product Name</th>
|
|
<th>Affiliate Link</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($products)): ?>
|
|
<tr>
|
|
<td colspan="3" class="text-center">No products found.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($products as $product): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($product['product_name']); ?></td>
|
|
<td><a href="<?php echo htmlspecialchars($product['affiliate_link']); ?>" target="_blank"><?php echo htmlspecialchars($product['affiliate_link']); ?></a></td>
|
|
<td>
|
|
<a href="products.php?action=delete&id=<?php echo $product['id']; ?>" class="btn btn-sm btn-danger-custom" onclick="return confirm('Are you sure you want to delete this product?');">
|
|
<i class="bi bi-trash"></i> Delete
|
|
</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="bg-dark text-white text-center py-4 mt-5">
|
|
<div class="container">
|
|
<p>© <?php echo date('Y'); ?> AI Reel Creator. All Rights Reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|