File manager - Edit - /home/webapp68.cm.in.th/u68319090006/Final/admin/add_article.php
Back
<?php session_start(); if (!isset($_SESSION['user_id']) || ($_SESSION['role'] != 'admin' && $_SESSION['role'] != 'teacher')) { header("Location: ../login.php"); exit(); } include(__DIR__ . '/../config/db.php'); // ดึงหมวดข่าว $categories = $conn->query("SELECT * FROM categories ORDER BY name ASC"); // ฟังก์ชันสร้าง slug ไม่ซ้ำ function generateSlug($title, $conn){ $slug = strtolower(preg_replace('/[^a-z0-9]+/i', '-', trim($title))); $slug = trim($slug, '-'); $baseSlug = $slug; $i = 1; while(true){ $stmt = $conn->prepare("SELECT id FROM articles WHERE slug=?"); $stmt->bind_param("s", $slug); $stmt->execute(); $result = $stmt->get_result(); if($result->num_rows == 0) break; $slug = $baseSlug . '-' . $i; $i++; } return $slug; } // บันทึกข่าวใหม่ if($_SERVER['REQUEST_METHOD'] == 'POST'){ $title = $_POST['title']; $content = $_POST['content']; $category_id = intval($_POST['category_id']); $status = $_POST['status']; $author_id = $_SESSION['user_id']; $slug = generateSlug($title, $conn); $stmt = $conn->prepare("INSERT INTO articles (title, slug, content, category_id, author_id, status, created_at) VALUES (?, ?, ?, ?, ?, ?, NOW())"); $stmt->bind_param("sssiss", $title, $slug, $content, $category_id, $author_id, $status); if($stmt->execute()){ header("Location: manage_articles.php"); exit(); } else { $error = "ไม่สามารถเพิ่มข่าวได้: ".$stmt->error; } $stmt->close(); } ?> <!DOCTYPE html> <html lang="th"> <head> <meta charset="UTF-8"> <title>เพิ่มข่าวใหม่</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #f5f6fa; margin: 0; padding: 0; } .container { max-width: 700px; margin: 40px auto; background: #fff; padding: 30px; border-radius: 10px; box-shadow: 0 0 15px rgba(0,0,0,0.1); } h2 { text-align: center; color: #333; margin-bottom: 25px; } a { color: #007bff; text-decoration: none; margin-right: 15px; } a:hover { text-decoration: underline; } label { display: block; margin-bottom: 6px; font-weight: bold; color: #555; } input[type="text"], select, textarea { width: 100%; padding: 10px 12px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 6px; font-size: 14px; } textarea { resize: vertical; } button { display: block; width: 100%; padding: 12px; background: #28a745; color: #fff; border: none; font-size: 16px; font-weight: bold; border-radius: 6px; cursor: pointer; transition: 0.3s; } button:hover { background: #218838; } .error { background: #f8d7da; color: #721c24; padding: 10px 15px; border-radius: 6px; margin-bottom: 20px; border: 1px solid #f5c6cb; } </style> </head> <body> <div class="container"> <h2>เพิ่มข่าวใหม่</h2> <a href="manage_articles.php">กลับหน้าจัดการข่าว</a> <a href="logout.php">ออกจากระบบ</a> <br><br> <?php if(isset($error)) echo "<div class='error'>$error</div>"; ?> <form method="post"> <label>หัวข้อข่าว:</label> <input type="text" name="title" placeholder="กรอกหัวข้อข่าว" required> <label>หมวดข่าว:</label> <select name="category_id" required> <?php while($row = $categories->fetch_assoc()): ?> <option value="<?php echo $row['id']; ?>"><?php echo htmlspecialchars($row['name']); ?></option> <?php endwhile; ?> </select> <label>เนื้อหาข่าว:</label> <textarea name="content" rows="10" placeholder="กรอกเนื้อหาข่าว" required></textarea> <label>สถานะ:</label> <select name="status"> <option value="draft">Draft</option> <option value="published" selected>Published</option> </select> <button type="submit">เพิ่มข่าว</button> </form> </div> </body> </html>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.86 |
proxy
|
phpinfo
|
Settings