« Back to History
posts_add.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php // File: /public_html/shayari/posts_add.php error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/config.php'; $pdo = db_connect(); $errors = []; $success = null; // ---------- small helper ---------- function slugify(string $text): string { $text = mb_strtolower($text, 'UTF-8'); $text = preg_replace('~[^\pL\d]+~u', '-', $text); $text = trim($text, '-'); $text = preg_replace('~[^-\w]+~', '', $text); if ($text === '') $text = 'post-' . time(); return $text; } /* ========== HANDLE FORM SUBMIT (ADD NEW POST) ========== */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $title = trim($_POST['title'] ?? ''); $slug = trim($_POST['slug'] ?? ''); $status = $_POST['status'] ?? 'published'; $body = trim($_POST['body_html'] ?? ''); $meta_t = trim($_POST['meta_title'] ?? ''); $meta_d = trim($_POST['meta_description'] ?? ''); $meta_k = trim($_POST['meta_keywords'] ?? ''); if ($title === '') { $errors[] = "Title required hai."; } if (!in_array($status, ['draft','published'], true)) { $status = 'published'; } if ($slug === '') { $slug = slugify($title); } else { $slug = slugify($slug); } if (empty($errors)) { try { // slug ko unique bana do $baseSlug = $slug; $i = 1; while (true) { $st = $pdo->prepare("SELECT id FROM posts WHERE slug = :slug LIMIT 1"); $st->execute([':slug' => $slug]); if (!$st->fetch()) break; $i++; $slug = $baseSlug . '-' . $i; } $sql = "INSERT INTO posts (title, slug, status, body_html, meta_title, meta_description, meta_keywords, created_at, updated_at) VALUES (:title, :slug, :status, :body_html, :meta_title, :meta_description, :meta_keywords, NOW(), NOW())"; $st = $pdo->prepare($sql); $st->execute([ ':title' => $title, ':slug' => $slug, ':status' => $status, ':body_html' => $body ?: null, ':meta_title' => $meta_t ?: null, ':meta_description'=> $meta_d ?: null, ':meta_keywords' => $meta_k ?: null, ]); $success = "Post saved! (ID: " . (int)$pdo->lastInsertId() . ")"; // sirf form clear karo, list waisi hi rahegi $title = $slug = $body = $meta_t = $meta_d = $meta_k = ''; $status = 'published'; } catch (Exception $e) { $errors[] = "Save error: " . htmlspecialchars($e->getMessage()); } } } /* ========== LOAD POSTS LIST ========== */ $posts = []; $st = $pdo->query("SELECT id, title, slug, status, created_at FROM posts ORDER BY created_at DESC"); $posts = $st->fetchAll(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Posts Add / Manage</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body{margin:0;font-family:system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;background:#f5f5f5;} .wrap{max-width:1100px;margin:20px auto;background:#fff;border-radius:8px;padding:20px;box-shadow:0 4px 12px rgba(0,0,0,0.08);} h1{margin-top:0;font-size:22px;} label{display:block;font-weight:600;font-size:13px;margin-bottom:4px;} input[type="text"], textarea, select{width:100%;padding:6px 8px;font-size:14px;margin-bottom:10px;} textarea{min-height:70px;} .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;} table{width:100%;border-collapse:collapse;font-size:13px;margin-top:15px;} th,td{border:1px solid #ddd;padding:6px 8px;vertical-align:top;} th{background:#f0f0f0;} .btn{padding:8px 14px;border:none;border-radius:4px;cursor:pointer;font-weight:600;font-size:13px;} .btn-primary{background:#34A853;color:#fff;} .status-pill{padding:2px 6px;border-radius:10px;font-size:11px;} .st-published{background:#E0F6E9;color:#1b5e20;} .st-draft{background:#FFF3CD;color:#856404;} a{color:#0066cc;text-decoration:none;} a:hover{text-decoration:underline;} @media (max-width:800px){ .row{flex-direction:column;} } .row{display:flex;gap:20px;flex-wrap:wrap;} .col{flex:1 1 340px;} </style> <script> function slugify(str){ return str.toLowerCase() .replace(/[^a-z0-9]+/g,'-') .replace(/^-+|-+$/g,''); } function autoSlug(){ var t = document.getElementById('title'); var s = document.getElementById('slug'); if(s.value.trim()===''){ s.value = slugify(t.value); } } </script> </head> <body> <div class="wrap"> <h1>Post Add / Manage (Love Shayari, Sad Shayari, ...)</h1> <?php if ($success): ?> <div class="msg-success"><?php echo htmlspecialchars($success); ?></div> <?php endif; ?> <?php if ($errors): ?> <div class="msg-error"> <ul style="margin:0;padding-left:18px;"> <?php foreach ($errors as $e): ?> <li><?php echo htmlspecialchars($e); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <div class="row"> <!-- LEFT: Add form --> <div class="col"> <h2 style="margin-top:0;">New Post</h2> <form method="post"> <label for="title">Title</label> <input type="text" id="title" name="title" value="<?php echo htmlspecialchars($title ?? ''); ?>" oninput="autoSlug()" required> <label for="slug">Slug (URL part)</label> <input type="text" id="slug" name="slug" value="<?php echo htmlspecialchars($slug ?? ''); ?>" placeholder="love-shayari"> <label for="status">Status</label> <select id="status" name="status"> <?php $stVal = $status ?? 'published'; ?> <option value="published" <?php echo $stVal==='published'?'selected':''; ?>>Published</option> <option value="draft" <?php echo $stVal==='draft'?'selected':''; ?>>Draft</option> </select> <label for="body_html">Intro / Content (optional)</label> <textarea id="body_html" name="body_html"><?php echo htmlspecialchars($body ?? ''); ?></textarea> <label for="meta_title">Meta Title (optional)</label> <input type="text" id="meta_title" name="meta_title" value="<?php echo htmlspecialchars($meta_t ?? ''); ?>"> <label for="meta_description">Meta Description (optional)</label> <textarea id="meta_description" name="meta_description"><?php echo htmlspecialchars($meta_d ?? ''); ?></textarea> <label for="meta_keywords">Meta Keywords (comma separated)</label> <input type="text" id="meta_keywords" name="meta_keywords" value="<?php echo htmlspecialchars($meta_k ?? ''); ?>"> <button type="submit" class="btn btn-primary">Save Post</button> </form> </div> <!-- RIGHT: post list --> <div class="col"> <h2 style="margin-top:0;">Existing Posts</h2> <table> <thead> <tr> <th>ID</th> <th>Title / Slug</th> <th>Status</th> <th>Created</th> </tr> </thead> <tbody> <?php if ($posts): ?> <?php foreach ($posts as $p): ?> <?php $status = $p['status']; $pill = $status==='published'?'st-published':'st-draft'; ?> <tr> <td><?php echo (int)$p['id']; ?></td> <td> <strong><?php echo htmlspecialchars($p['title']); ?></strong><br> <small><?php echo htmlspecialchars($p['slug']); ?></small> </td> <td> <span class="status-pill <?php echo $pill; ?>"> <?php echo htmlspecialchars($status); ?> </span> </td> <td><small><?php echo htmlspecialchars($p['created_at']); ?></small></td> </tr> <?php endforeach; ?> <?php else: ?> <tr><td colspan="4">Abhi koi post nahi hai.</td></tr> <?php endif; ?> </tbody> </table> </div> </div> </div> </body> </html>