« Back to History
shayari_add.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php // ================== CONFIG / DB CONNECT ================== $DB_HOST = 'localhost'; $DB_NAME = 'u410576661_shayari'; $DB_USER = 'u410576661_shayari'; $DB_PASS = 'Shayari@9898'; $dsn = "mysql:host={$DB_HOST};dbname={$DB_NAME};charset=utf8mb4"; try { $pdo = new PDO($dsn, $DB_USER, $DB_PASS, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); } catch (PDOException $e) { die("DB connection failed: " . htmlspecialchars($e->getMessage())); } // ================== HELPERS ================== function normalize_text(string $text): string { // newline -> space $text = str_replace(["\r\n", "\n", "\r"], " ", $text); // lowercase $text = mb_strtolower($text, 'UTF-8'); // remove punctuation (basic) $text = preg_replace('/[[:punct:]]+/u', ' ', $text); // multiple spaces -> single $text = preg_replace('/\s+/u', ' ', $text); // trim $text = trim($text); return $text; } // ================== LOAD CATEGORIES FOR DROPDOWN ================== $categories = []; try { $stmt = $pdo->query("SELECT id, name FROM shayari_categories ORDER BY sort_order ASC, name ASC"); $categories = $stmt->fetchAll(); } catch (Exception $e) { // agar category table empty ya nahi hai to bhi page chale $categories = []; } // ================== FORM HANDLE ================== $errors = []; $success = null; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $shayari_text = trim($_POST['shayari_text'] ?? ''); $category_id = isset($_POST['category_id']) && $_POST['category_id'] !== '' ? (int)$_POST['category_id'] : null; $tags_raw = trim($_POST['tags'] ?? ''); // comma separated $status = $_POST['status'] ?? 'active'; // ---- Validate ---- if ($shayari_text === '') { $errors[] = "Shayari text required hai."; } if (!in_array($status, ['active', 'draft', 'archived'], true)) { $status = 'active'; } // ---- Normalize + duplicate check ---- $normalized_text = normalize_text($shayari_text); $normalized_hash = $normalized_text !== '' ? md5($normalized_text) : null; if ($normalized_hash) { $dupStmt = $pdo->prepare("SELECT id, text FROM shayari WHERE normalized_hash = :h LIMIT 5"); $dupStmt->execute([':h' => $normalized_hash]); $exactDuplicates = $dupStmt->fetchAll(); if ($exactDuplicates) { $errors[] = "Ye shayari pehle se database me exact same form me maujood hai. (normalized_hash match)"; } } // ---- Image upload handle ---- $image_path = null; $upload_dir = __DIR__ . '/uploads/shayari_images'; // make sure folder exists & writable if (!is_dir($upload_dir)) { // try to create directory if not exist @mkdir($upload_dir, 0775, true); } if (!empty($_FILES['image']['name'])) { if (!is_dir($upload_dir) || !is_writable($upload_dir)) { $errors[] = "Image upload folder writeable nahi hai."; } else { $file = $_FILES['image']; if ($file['error'] === UPLOAD_ERR_OK) { $tmp_name = $file['tmp_name']; $orig_name = $file['name']; // basic extension check $ext = strtolower(pathinfo($orig_name, PATHINFO_EXTENSION)); $allowed = ['jpg', 'jpeg', 'png', 'webp', 'gif']; if (!in_array($ext, $allowed, true)) { $errors[] = "Sirf image files allowed hain (jpg, png, webp, gif)."; } else { $new_name = 'shayari_' . time() . '_' . bin2hex(random_bytes(4)) . '.' . $ext; $dest_path = $upload_dir . '/' . $new_name; if (move_uploaded_file($tmp_name, $dest_path)) { // DB me relative path store karein $image_path = 'uploads/shayari_images/' . $new_name; } else { $errors[] = "Image upload fail ho gaya."; } } } else { $errors[] = "Image upload error code: " . $file['error']; } } } // ---- If no errors, insert ---- if (empty($errors)) { try { $pdo->beginTransaction(); $insertSql = "INSERT INTO shayari (text, category_id, default_image, normalized_text, normalized_hash, status, created_at, updated_at) VALUES (:text, :category_id, :default_image, :normalized_text, :normalized_hash, :status, NOW(), NOW())"; $stmt = $pdo->prepare($insertSql); $stmt->execute([ ':text' => $shayari_text, ':category_id' => $category_id, ':default_image' => $image_path, ':normalized_text' => $normalized_text, ':normalized_hash' => $normalized_hash, ':status' => $status, ]); $shayari_id = (int)$pdo->lastInsertId(); // ---- Handle tags ---- $tags = []; if ($tags_raw !== '') { // "love, true love, dosti" -> ["love","true love","dosti"] $tags = array_filter(array_map('trim', explode(',', $tags_raw)), function ($t) { return $t !== ''; }); } if ($tags) { $tagSelect = $pdo->prepare("SELECT id FROM tags WHERE name = :name"); $tagInsert = $pdo->prepare("INSERT INTO tags (name, created_at) VALUES (:name, NOW())"); $mapInsert = $pdo->prepare(" INSERT IGNORE INTO shayari_tags (shayari_id, tag_id) VALUES (:shayari_id, :tag_id) "); foreach ($tags as $tagName) { // 1) kya tag exist karta hai? $tagSelect->execute([':name' => $tagName]); $tagRow = $tagSelect->fetch(); if ($tagRow) { $tagId = (int)$tagRow['id']; } else { $tagInsert->execute([':name' => $tagName]); $tagId = (int)$pdo->lastInsertId(); } // 2) mapping insert $mapInsert->execute([ ':shayari_id' => $shayari_id, ':tag_id' => $tagId, ]); } } $pdo->commit(); $success = "Shayari saved! (ID: {$shayari_id})"; $shayari_text = ''; // bus shayari text blank hoga // $category_id = null; // $tags_raw = ''; // $status = 'active'; } catch (Exception $e) { $pdo->rollBack(); $errors[] = "Save ke time error: " . htmlspecialchars($e->getMessage()); } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Add Shayari</title> <style> body { font-family: system-ui, sans-serif; background:#f5f5f5; margin:0; padding:20px; } .wrap { max-width: 800px; 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 textarea { width:100%; min-height:120px; padding:8px; } .field input[type="text"], .field select { 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; } .errors ul { margin:0; padding-left:18px; } button { padding:10px 18px; border:none; border-radius:4px; cursor:pointer; font-weight:600; } button[type="submit"] { background:#34A853; color:white; } </style> </head> <body> <div class="wrap"> <h1>Shayari Add / Insert</h1> <?php if ($success): ?> <div class="msg-success"><?php echo htmlspecialchars($success); ?></div> <?php endif; ?> <?php if ($errors): ?> <div class="msg-error errors"> <strong>Errors:</strong> <ul> <?php foreach ($errors as $e): ?> <li><?php echo htmlspecialchars($e); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <form method="post" enctype="multipart/form-data"> <div class="field"> <label for="shayari_text">Shayari Text</label> <textarea name="shayari_text" id="shayari_text" required><?php echo isset($shayari_text) ? htmlspecialchars($shayari_text) : ''; ?></textarea> </div> <div class="field"> <label for="category_id">Category (optional)</label> <select name="category_id" id="category_id"> <option value="">-- Select Category --</option> <?php foreach ($categories as $cat): ?> <option value="<?php echo (int)$cat['id']; ?>" <?php echo (isset($category_id) && (int)$category_id === (int)$cat['id']) ? 'selected' : ''; ?>> <?php echo htmlspecialchars($cat['name']); ?> </option> <?php endforeach; ?> </select> </div> <div class="field"> <label for="tags">Tags (comma separated)</label> <input type="text" name="tags" id="tags" placeholder="love, true love, dosti" value="<?php echo isset($tags_raw) ? htmlspecialchars($tags_raw) : ''; ?>"> </div> <div class="field"> <label for="image">Image (optional)</label> <input type="file" name="image" id="image" accept="image/*"> <small>JPG / PNG / WEBP / GIF allowed.</small> </div> <div class="field"> <label for="status">Status</label> <select name="status" id="status"> <option value="active" <?php echo (isset($status) && $status === 'active') ? 'selected' : ''; ?>>Active</option> <option value="draft" <?php echo (isset($status) && $status === 'draft') ? 'selected' : ''; ?>>Draft</option> <option value="archived" <?php echo (isset($status) && $status === 'archived') ? 'selected' : ''; ?>>Archived</option> </select> </div> <button type="submit">Save Shayari</button> </form> </div> </body> </html>