Type Here to Get Search Results !

CREATE TABLE IF NOT EXISTS posts

connect_error) { die("Connection failed: " . $conn->connect_error); } // Create table if not exists $sql = "CREATE TABLE IF NOT EXISTS posts ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, author VARCHAR(100) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, image_url VARCHAR(255) )"; $conn->query($sql); // Handle form submissions if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST['action'])) { switch ($_POST['action']) { case 'create': createPost(); break; case 'update': updatePost(); break; case 'delete': deletePost(); break; } } } // Create new post function createPost() { global $conn; $title = $_POST['title']; $content = $_POST['content']; $author = $_POST['author']; $image_url = ''; // Handle image upload if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) { $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["image"]["name"]); if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) { $image_url = $target_file; } } $stmt = $conn->prepare("INSERT INTO posts (title, content, author, image_url) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssss", $title, $content, $author, $image_url); $stmt->execute(); $stmt->close(); header("Location: index.php"); } // Update existing post function updatePost() { global $conn; $id = $_POST['id']; $title = $_POST['title']; $content = $_POST['content']; $stmt = $conn->prepare("UPDATE posts SET title=?, content=? WHERE id=?"); $stmt->bind_param("ssi", $title, $content, $id); $stmt->execute(); $stmt->close(); header("Location: index.php"); } // Delete post function deletePost() { global $conn; $id = $_POST['id']; $stmt = $conn->prepare("DELETE FROM posts WHERE id=?"); $stmt->bind_param("i", $id); $stmt->execute(); $stmt->close(); header("Location: index.php"); } // Get all posts function getPosts() { global $conn; $result = $conn->query("SELECT * FROM posts ORDER BY created_at DESC"); return $result->fetch_all(MYSQLI_ASSOC); } // Get single post function getPost($id) { global $conn; $stmt = $conn->prepare("SELECT * FROM posts WHERE id=?"); $stmt->bind_param("i", $id); $stmt->execute(); $result = $stmt->get_result(); return $result->fetch_assoc(); } ?> Blog Post Management

Blog Post Management

Create New Post

All Posts

"; echo "

" . htmlspecialchars($post['title']) . "

"; if ($post['image_url']) { echo ""; } echo "

" . nl2br(htmlspecialchars($post['content'])) . "

"; echo "

By " . htmlspecialchars($post['author']) . " on " . $post['created_at'] . "

"; // Edit form echo "
"; echo ""; echo ""; echo ""; echo "
"; // Delete form echo "
"; echo ""; echo ""; echo ""; echo "
"; echo "
"; } ?>

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.