๐ฏ Learning Objectives
- Complete edit and delete functionality
- Implement search feature
- Add security measures (SQL injection prevention)
- Test entire application
- Deploy and celebrate completion! ๐
โ๏ธ 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:
- โ View homepage - all posts display correctly
- โ Click on post - view full content
- โ View counter increments when viewing post
- โ Create new post - saves to database
- โ Edit post - changes are saved
- โ Delete post - removes from database
- โ Search functionality works
- โ Validation errors display correctly
- โ Success messages show after actions
- โ All links work properly
๐ Deployment Steps
- Local Testing: Test everything on localhost first
- Database Backup: Export your database from phpMyAdmin
- Choose Hosting: Options include:
- InfinityFree (Free PHP hosting)
- 000webhost (Free PHP hosting)
- Hostinger (Affordable paid option)
- Upload Files: Use FTP to upload project files
- Import Database: Create database on server and import SQL
- Update Config: Change database credentials in config/database.php
- 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
- Build More Projects: Portfolio website, e-commerce site, social network
- Learn Advanced Topics: User authentication, file uploads, APIs
- Explore Frameworks: Laravel (PHP), React (JavaScript)
- Join Communities: Stack Overflow, GitHub, Dev.to
- Keep Coding: Practice daily, even small projects count!
โ Final Day Deliverable
- โ Complete blog with all CRUD operations
- โ Edit post functionality working
- โ Delete post with confirmation
- โ Search feature implemented
- โ All security measures in place
- โ Full testing completed
- โ Blog deployed (optional but recommended)
๐ Course Completed!
You are now a certified Full Stack Web Developer!
Keep building, keep learning, and keep growing! ๐