php mysql blog code
Posted by Superadmin on September 06 2024 05:27:31

Creating a blog using PHP and MySQL is a great project to get hands-on experience with web development. Here’s a basic outline to get you started:

1. Setting Up Your Project

  1. Create a project folder: Name it something like blog.
  2. Inside the project folder, create subfolders:

2. Database Setup

Create a MySQL database and a table for your blog posts. Here’s a simple SQL script to create a posts table:

SQL
CREATE DATABASE blog_db;
USE blog_db;

CREATE TABLE posts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    author VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

3. Basic PHP Code

Create an index.php file in your project root:

Welcome to My Blog

num_rows > 0) { while($row = $result->fetch_assoc()) { echo "

"; echo "

" . $row["title"] . "

"; echo "

" . $row["content"] . "

"; echo "

by " . $row["author"] . " on " . $row["created_at"] . "

"; echo "

"; } } else { echo "0 results"; } $conn->close(); ?> " style="position: relative; display: block; border: 1px solid var(--cib-color-stroke-neutral-primary); border-radius: var(--cib-border-radius-large); clear: both; margin-top: 12px; margin-block: 24px 12px; color: rgb(17, 17, 17); font-family: -apple-system, Roboto, SegoeUI, "Segoe UI", "Helvetica Neue", Helvetica, "Microsoft YaHei", "Meiryo UI", Meiryo, "Arial Unicode MS", sans-serif; font-size: 16px;">

PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "blog_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, title, content, author, created_at FROM posts";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
    <title>My Blog</title>
    <link rel="stylesheet" type="text/css" href="static/css/style.css">
</head>
<body>
    <h1>Welcome to My Blog</h1>
    <?php
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "<div class='post'>";
            echo "<h2>" . $row["title"] . "</h2>";
            echo "<p>" . $row["content"] . "</p>";
            echo "<p><em>by " . $row["author"] . " on " . $row["created_at"] . "</em></p>";
            echo "</div>";
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>
</body>
</html>

4. Admin Panel

Create an admin folder and add files for creating, editing, and deleting posts. For example, create_post.php:

PHP
<?php
// Include database connection
include('../includes/db_connect.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST['title'];
    $content = $_POST['content'];
    $author = $_POST['author'];

    $sql = "INSERT INTO posts (title, content, author) VALUES ('$title', '$content', '$author')";

    if ($conn->query($sql) === TRUE) {
        echo "New post created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Create Post</title>
</head>
<body>
    <h1>Create a New Post</h1>
    <form method="post" action="create_post.php">
        <label for="title">Title:</label><br>
        <input type="text" id="title" name="title"><br>
        <label for="content">Content:</label><br>
        <textarea id="content" name="content"></textarea><br>
        <label for="author">Author:</label><br>
        <input type="text" id="author" name="author"><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

This is a basic setup to get you started. You can find more detailed tutorials and complete projects on platforms like GitHub1 and CodeWithAwa2. Happy coding! 😊