« Back to History
category_add.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php // File: /public_html/shayari/category_add.php error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/config.php'; $pdo = db_connect(); $errors = []; $success = null; // ---------- PHP slugify helper ---------- function slugify(string $text): string { // lower $text = mb_strtolower($text, 'UTF-8'); // non letters/digits -> - $text = preg_replace('~[^\pL\d]+~u', '-', $text); // trim - $text = trim($text, '-'); // remove unwanted chars $text = preg_replace('~[^-\w]+~', '', $text); if ($text === '') { $text = 'cat-' . time(); } return $text; } // ---------- Handle POST ---------- $name = trim($_POST['name'] ?? ''); $slug_input = trim($_POST['slug'] ?? ''); $sort_order = isset($_POST['sort_order']) ? (int)$_POST['sort_order'] : 0; if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($name === '') { $errors[] = "Category name required hai."; } // slug generate if empty $slug = $slug_input !== '' ? $slug_input : slugify($name); if (empty($errors)) { try { // ensure slug unique: agar clash ho to -2, -3 ... add karo $baseSlug = $slug; $i = 1; while (true) { $st = $pdo->prepare("SELECT id FROM shayari_categories WHERE slug = :slug LIMIT 1"); $st->execute([':slug' => $slug]); $existing = $st->fetch(); if (!$existing) { break; // slug free } $i++; $slug = $baseSlug . '-' . $i; } $sql = "INSERT INTO shayari_categories (name, slug, sort_order, created_at, updated_at) VALUES (:name, :slug, :sort_order, NOW(), NOW())"; $ins = $pdo->prepare($sql); $ins->execute([ ':name' => $name, ':slug' => $slug, ':sort_order' => $sort_order, ]); $success = "Category saved! (ID: " . (int)$pdo->lastInsertId() . ")"; // sirf name reset karo, slug field blank rakho, sort_order wahi rehne do $name = ''; $slug_input = ''; } catch (Exception $e) { $errors[] = "Save error: " . htmlspecialchars($e->getMessage()); } } } // ---------- Existing categories list ---------- $categories = []; try { $st = $pdo->query("SELECT id, name, slug, sort_order FROM shayari_categories ORDER BY sort_order ASC, name ASC"); $categories = $st->fetchAll(); } catch (Exception $e) { $errors[] = "Category list load error: " . htmlspecialchars($e->getMessage()); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Add Category</title> <style> body { font-family: system-ui, sans-serif; background:#f5f5f5; margin:0; padding:20px; } .wrap { max-width: 900px; margin:0 auto; background:#fff; padding:20px; border-radius:8px; box-shadow:0 0 8px rgba(0,0,0,0.06); } h1 { margin-top:0; } .field { margin-bottom:16px; } .field label { display:block; font-weight:600; margin-bottom:6px; } .field input { width:100%; padding:8px; } .msg-error { background:#ffe2e2; border:1px solid #f5aaaa; padding:10px; margin-bottom:12px; border-radius:6px; } .msg-success { background:#e2ffe7; border:1px solid #aaf5bc; padding:10px; margin-bottom:12px; border-radius:6px; } ul { margin:0; padding-left:18px; } button { padding:10px 18px; border:none; border-radius:4px; cursor:pointer; font-weight:600; background:#34A853; color:#fff; } table { width:100%; border-collapse:collapse; margin-top:20px; font-size:14px; } th, td { border:1px solid #ddd; padding:6px 8px; text-align:left; } th { background:#f0f0f0; } </style> <script> // JS: name se slug auto fill (sirf UI ke liye, actual safety PHP side hai) function slugify(str) { return str .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); } function updateSlug() { var name = document.getElementById('name').value; var slugField = document.getElementById('slug'); if (slugField.value.trim() === '') { slugField.value = slugify(name); } } </script> </head> <body> <div class="wrap"> <h1>Add New Category</h1> <?php if ($success): ?> <div class="msg-success"><?php echo htmlspecialchars($success); ?></div> <?php endif; ?> <?php if ($errors): ?> <div class="msg-error"> <strong>Errors:</strong> <ul> <?php foreach ($errors as $e): ?> <li><?php echo htmlspecialchars($e); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <form method="post"> <div class="field"> <label for="name">Category Name</label> <input type="text" name="name" id="name" value="<?php echo htmlspecialchars($name); ?>" oninput="updateSlug()" required> </div> <div class="field"> <label for="slug">Slug (optional – blank chhodo to auto banega)</label> <input type="text" name="slug" id="slug" value="<?php echo htmlspecialchars($slug_input); ?>"> </div> <div class="field"> <label for="sort_order">Sort Order (optional)</label> <input type="number" name="sort_order" id="sort_order" value="<?php echo (int)$sort_order; ?>"> </div> <button type="submit">Save Category</button> </form> <h2>Existing Categories</h2> <?php if ($categories): ?> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Slug</th> <th>Sort Order</th> </tr> </thead> <tbody> <?php foreach ($categories as $c): ?> <tr> <td><?php echo (int)$c['id']; ?></td> <td><?php echo htmlspecialchars($c['name']); ?></td> <td><?php echo htmlspecialchars($c['slug']); ?></td> <td><?php echo (int)$c['sort_order']; ?></td> </tr> <?php endforeach; ?> </tbody> </table> <?php else: ?> <p>Abhi koi category nahi hai.</p> <?php endif; ?> </div> </body> </html>