In what ways can a mini-blog system be a suitable project for a student looking to showcase their PHP skills for an exam?
A mini-blog system can be a suitable project for a student looking to showcase their PHP skills for an exam because it allows them to demonstrate their ability to create a dynamic website with user authentication, CRUD functionality, and data validation. By implementing features such as user registration, login, posting, editing, and deleting blog posts, as well as commenting on posts, students can showcase their proficiency in PHP programming and database management.
<?php
// Sample PHP code for a mini-blog system showcasing PHP skills
// Database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// User authentication
session_start();
if(isset($_SESSION['user_id'])){
// User is logged in
} else {
// Redirect to login page
header("Location: login.php");
exit();
}
// CRUD functionality
// Add new blog post
if(isset($_POST['submit'])){
$title = $_POST['title'];
$content = $_POST['content'];
$sql = "INSERT INTO posts (title, content) VALUES ('$title', '$content')";
$conn->query($sql);
}
// Edit blog post
if(isset($_POST['edit'])){
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$sql = "UPDATE posts SET title='$title', content='$content' WHERE id=$id";
$conn->query($sql);
}
// Delete blog post
if(isset($_POST['delete'])){
$id = $_POST['id'];
$sql = "DELETE FROM posts WHERE id=$id";
$conn->query($sql);
}
// Data validation
function validate_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$conn->close();
?>