Alfaaz-E-Dil / Collection
Not Found yet #116
<?php
session_start();
if (empty($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
header('Location: login.php');
exit;
}

require_once __DIR__ . '/db.php';

$error = '';
$success = '';
$categories = $pdo->query("SELECT * FROM categories ORDER BY name ASC")->fetchAll();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$category_id = isset($_POST['category_id']) ? (int)$_POST['category_id'] : 0;
$new_category = trim($_POST['new_category'] ?? '');
$content = trim($_POST['content'] ?? '');

if ($content === '') {
$error = 'Please write your Shayari before publishing.';
} else {
try {
// Create new category if provided
if ($new_category !== '') {
$check = $pdo->prepare("SELECT id FROM categories WHERE name = ?");
$check->execute(array($new_category));
$existing = $check->fetch();
if ($existing) {
$category_id = (int)$existing['id'];
} else {
$insCat = $pdo->prepare("INSERT INTO categories (name) VALUES (?)");
$insCat->execute(array($new_category));
$category_id = (int)$pdo->lastInsertId();
}
}

if ($category_id <= 0) {
$error = 'Please select or enter a category.';
} else {
$stmt = $pdo->prepare("INSERT INTO shayaris (category_id, content, author) VALUES (?, ?, ?)");
$stmt->execute(array($category_id, $content, 'Alfaaz E Dil'));
header('Location: index.php');
exit;
}
} catch (Exception $e) {
$error = 'Error publishing: ' . $e->getMessage();
}
}
}

$pageTitle = 'Publish Shayari — Alfaaz-E-Dil';
$breadcrumbTitle = 'Write Shayari';
require_once __DIR__ . '/includes/header.php';
?>

<div class="page-card">
<h1 class="page-card-title">Write New Shayari</h1>
<p class="page-card-sub">Publish Hindi, Urdu, or Hinglish verses directly to <?= htmlspecialchars(SITE_NAME, ENT_QUOTES, 'UTF-8') ?>.</p>

<?php if ($error !== ''): ?>
<div class="alert alert-error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>

<form method="POST" action="add.php">
<div class="form-group">
<label class="form-label" for="category_id">Select Category</label>
<select id="category_id" name="category_id" class="form-control">
<option value="0">-- Choose Existing Category --</option>
<?php foreach ($categories as $cat): ?>
<option value="<?= (int)$cat['id'] ?>"><?= htmlspecialchars($cat['name'], ENT_QUOTES, 'UTF-8') ?></option>
<?php endforeach; ?>
</select>
</div>

<div class="form-group">
<label class="form-label" for="new_category">Or Create New Category (Optional)</label>
<input type="text" id="new_category" name="new_category" class="form-control" placeholder="e.g. Dard, Motivation, Monsoon...">
</div>

<div class="form-group">
<label class="form-label" for="content">Shayari</label>
<textarea id="content" name="content" rows="7" class="form-control" placeholder="लिखिए अपने दिल की बात..." required></textarea>
</div>

<button type="submit" class="btn-submit">Publish Shayari</button>
</form>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
Link ready to share!