๐Ÿ“… Week 12 - Day 3: Blog Finalization & Deployment

Friday | Duration: 3 hours | Final Day! ๐ŸŽ‰๐ŸŽ“

๐ŸŽฏ Learning Objectives

โœ๏ธ Edit Post (edit_post.php)

<?php // edit_post.php require_once 'config/database.php'; // Get post ID $post_id = isset($_GET['id']) ? (int)$_GET['id'] : 0; // Fetch post $stmt = mysqli_prepare($conn, "SELECT * FROM posts WHERE id = ?"); mysqli_stmt_bind_param($stmt, "i", $post_id); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); $post = mysqli_fetch_assoc($result); if (!$post) { header("Location: index.php"); exit(); } $pageTitle = "Edit: " . htmlspecialchars($post['title']); $errors = []; $success = ""; // Handle form submission if ($_SERVER["REQUEST_METHOD"] == "POST") { $title = trim($_POST['title']); $content = trim($_POST['content']); $author = trim($_POST['author']); $category = trim($_POST['category']); // Validation if (empty($title) || strlen($title) < 5) { $errors[] = "Title must be at least 5 characters"; } if (empty($content) || strlen($content) < 20) { $errors[] = "Content must be at least 20 characters"; } if (empty($author)) { $errors[] = "Author name is required"; } // If no errors, update if (empty($errors)) { $updateStmt = mysqli_prepare($conn, "UPDATE posts SET title = ?, content = ?, author = ?, category = ? WHERE id = ?"); mysqli_stmt_bind_param($updateStmt, "ssssi", $title, $content, $author, $category, $post_id); if (mysqli_stmt_execute($updateStmt)) { $success = "Post updated successfully!"; // Update local post data $post['title'] = $title; $post['content'] = $content; $post['author'] = $author; $post['category'] = $category; } else { $errors[] = "Error updating post"; } mysqli_stmt_close($updateStmt); } } include 'includes/header.php'; ?> <div class="container"> <div class="post-card"> <h2 style="color: #f5576c; margin-bottom: 30px;">โœ๏ธ Edit Blog Post</h2> <?php if ($success): ?> <div style="background: #d4edda; color: #155724; padding: 15px; border-radius: 5px; margin-bottom: 20px;"> <?php echo $success; ?> </div> <?php endif; ?> <?php if (!empty($errors)): ?> <div style="background: #f8d7da; color: #721c24; padding: 15px; border-radius: 5px; margin-bottom: 20px;"> <ul style="margin-left: 20px;"> <?php foreach ($errors as $error): ?> <li><?php echo htmlspecialchars($error); ?></li> <?php endforeach; ?> </ul> </div> <?php endif; ?> <form method="POST"> <div style="margin-bottom: 20px;"> <label style="display: block; margin-bottom: 5px; font-weight: bold;">Post Title:</label> <input type="text" name="title" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px;" value="<?php echo htmlspecialchars($post['title']); ?>" required> </div> <div style="margin-bottom: 20px;"> <label style="display: block; margin-bottom: 5px; font-weight: bold;">Author Name:</label> <input type="text" name="author" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px;" value="<?php echo htmlspecialchars($post['author']); ?>" required> </div> <div style="margin-bottom: 20px;"> <label style="display: block; margin-bottom: 5px; font-weight: bold;">Category:</label> <select name="category" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px;"> <option value="">-- Select Category --</option> <option value="Technology" <?php echo $post['category'] == 'Technology' ? 'selected' : ''; ?>>Technology</option> <option value="Career" <?php echo $post['category'] == 'Career' ? 'selected' : ''; ?>>Career</option> <option value="Personal" <?php echo $post['category'] == 'Personal' ? 'selected' : ''; ?>>Personal</option> <option value="Tutorial" <?php echo $post['category'] == 'Tutorial' ? 'selected' : ''; ?>>Tutorial</option> <option value="General" <?php echo $post['category'] == 'General' ? 'selected' : ''; ?>>General</option> </select> </div> <div style="margin-bottom: 20px;"> <label style="display: block; margin-bottom: 5px; font-weight: bold;">Content:</label> <textarea name="content" rows="12" style="width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 5px; resize: vertical;" required><?php echo htmlspecialchars($post['content']); ?></textarea> </div> <button type="submit" class="btn">๐Ÿ’พ Save Changes</button> <a href="view_post.php?id=<?php echo $post_id; ?>" class="btn" style="background: #6c757d; text-decoration: none;">Cancel</a> </form> </div> </div> <?php mysqli_close($conn); include 'includes/footer.php'; ?>

๐Ÿ—‘๏ธ Delete Post (delete_post.php)

<?php // delete_post.php require_once 'config/database.php'; // Get post ID $post_id = isset($_GET['id']) ? (int)$_GET['id'] : 0; if ($post_id > 0) { // Delete post $stmt = mysqli_prepare($conn, "DELETE FROM posts WHERE id = ?"); mysqli_stmt_bind_param($stmt, "i", $post_id); if (mysqli_stmt_execute($stmt)) { if (mysqli_stmt_affected_rows($stmt) > 0) { $message = "Post deleted successfully!"; } else { $message = "Post not found."; } } else { $message = "Error deleting post."; } mysqli_stmt_close($stmt); } else { $message = "Invalid post ID."; } mysqli_close($conn); // Redirect to homepage with message header("Location: index.php?message=" . urlencode($message)); exit(); ?>

๐Ÿ” Search Functionality (search.php)

<?php // search.php $pageTitle = "Search Results - My Blog"; require_once 'config/database.php'; $searchTerm = isset($_GET['q']) ? trim($_GET['q']) : ''; include 'includes/header.php'; ?> <div class="container"> <!-- Search Form --> <div class="post-card"> <form method="GET" action="search.php" style="display: flex; gap: 10px;"> <input type="text" name="q" placeholder="Search posts..." style="flex: 1; padding: 12px; border: 1px solid #ddd; border-radius: 5px;" value="<?php echo htmlspecialchars($searchTerm); ?>"> <button type="submit" class="btn">๐Ÿ” Search</button> </form> </div> <?php if (!empty($searchTerm)) { echo "<h2 style='margin: 30px 0 20px;'>Search Results for: \"" . htmlspecialchars($searchTerm) . "\"</h2>"; // Search in title and content $search = "%{$searchTerm}%"; $stmt = mysqli_prepare($conn, "SELECT * FROM posts WHERE title LIKE ? OR content LIKE ? ORDER BY created_at DESC"); mysqli_stmt_bind_param($stmt, "ss", $search, $search); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); if (mysqli_num_rows($result) > 0) { while ($post = mysqli_fetch_assoc($result)) { $excerpt = substr($post['content'], 0, 150) . '...'; ?> <div class="post-card"> <h3 style="color: #f5576c; margin-bottom: 10px;"> <a href="view_post.php?id=<?php echo $post['id']; ?>" style="text-decoration: none; color: inherit;"> <?php echo htmlspecialchars($post['title']); ?> </a> </h3> <div style="color: #666; font-size: 14px; margin-bottom: 15px;"> <span>โœ๏ธ <?php echo htmlspecialchars($post['author']); ?></span> <span style="margin: 0 10px;">|</span> <span>๐Ÿ“… <?php echo date('F j, Y', strtotime($post['created_at'])); ?></span> </div> <p style="color: #555; margin-bottom: 15px;"> <?php echo htmlspecialchars($excerpt); ?> </p> <a href="view_post.php?id=<?php echo $post['id']; ?>" class="btn">Read More โ†’</a> </div> <?php } } else { echo "<p>No posts found matching your search.</p>"; } mysqli_stmt_close($stmt); } else { echo "<p>Please enter a search term.</p>"; } mysqli_close($conn); ?> </div> <?php include 'includes/footer.php'; ?>

๐Ÿ”’ Security Best Practices Implemented

โœ… Security Checklist:

  • โœ“ Prepared Statements: All database queries use prepared statements to prevent SQL injection
  • โœ“ Input Validation: All user inputs are validated before processing
  • โœ“ Output Escaping: Using htmlspecialchars() to prevent XSS attacks
  • โœ“ Type Casting: IDs are cast to integers: (int)$_GET['id']
  • โœ“ Sanitization: Using trim() to remove extra whitespace
  • โœ“ Confirmation: JavaScript confirm() before deleting

๐Ÿงช Testing Checklist

Test All Features:

  1. โœ“ View homepage - all posts display correctly
  2. โœ“ Click on post - view full content
  3. โœ“ View counter increments when viewing post
  4. โœ“ Create new post - saves to database
  5. โœ“ Edit post - changes are saved
  6. โœ“ Delete post - removes from database
  7. โœ“ Search functionality works
  8. โœ“ Validation errors display correctly
  9. โœ“ Success messages show after actions
  10. โœ“ All links work properly

๐Ÿš€ Deployment Steps

  1. Local Testing: Test everything on localhost first
  2. Database Backup: Export your database from phpMyAdmin
  3. Choose Hosting: Options include:
    • InfinityFree (Free PHP hosting)
    • 000webhost (Free PHP hosting)
    • Hostinger (Affordable paid option)
  4. Upload Files: Use FTP to upload project files
  5. Import Database: Create database on server and import SQL
  6. Update Config: Change database credentials in config/database.php
  7. Test Live: Visit your live site and test all features

๐ŸŽฏ Final Project Requirements

Feature Status
Database with posts table โœ…
Homepage listing all posts โœ…
View single post page โœ…
Create new post โœ…
Edit existing post โœ…
Delete post โœ…
Search functionality โœ…
View counter โœ…
Input validation โœ…
Security measures โœ…
Clean, professional design โœ…

๐ŸŽŠ Congratulations! You Did It! ๐ŸŽ‰

You've completed the 12-Week Full Stack Web Development Bootcamp!

You've learned:

  • โœจ HTML5 & Semantic Structure
  • ๐ŸŽจ CSS3 & Responsive Design
  • ๐Ÿ“ฑ Bootstrap Framework
  • โšก JavaScript & DOM Manipulation
  • ๐Ÿ˜ PHP Backend Development
  • ๐Ÿ—„๏ธ MySQL Databases & SQL
  • ๐Ÿ”’ Security Best Practices
  • ๐Ÿš€ Full CRUD Applications

You're now a Full Stack Web Developer!

๐Ÿ“š Next Steps in Your Journey

  1. Build More Projects: Portfolio website, e-commerce site, social network
  2. Learn Advanced Topics: User authentication, file uploads, APIs
  3. Explore Frameworks: Laravel (PHP), React (JavaScript)
  4. Join Communities: Stack Overflow, GitHub, Dev.to
  5. Keep Coding: Practice daily, even small projects count!

โœ… Final Day Deliverable

๐ŸŽ“ Course Completed!

You are now a certified Full Stack Web Developer!

Keep building, keep learning, and keep growing! ๐Ÿš€

โ† Previous ๐Ÿ  Back to Course Home