Can Anybody Help Me?

post. php

<?php
class Post {
    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function create($title, $link) {
        // Generate a random slug between 5 and 8 characters (case-sensitive)
        $slug = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'), 0, rand(4, 6));

        // Insert data into database
        $query = "INSERT INTO posts (title, link, slug) VALUES (:title, :link, :slug)";
        $params = [
            ':title' => $title,
            ':link' => $link,
            ':slug' => $slug,
        ];
        $this->db->executeQuery($query, $params);
        return $slug;
    }

    public function getAll() {
        $query = "SELECT * FROM posts ORDER BY created_at DESC";
        return $this->db->executeQuery($query);
    }

    public function getBySlug($slug) {
        $query = "SELECT * FROM posts WHERE slug = :slug";
        $params = [
            ':slug' => $slug,
        ];
        $result = $this->db->executeQuery($query, $params);
        if (count($result) > 0) {
            return $result[0];
        } else {
            return null;
        }
    }

    public function update($slug, $title, $link) {
        $query = "UPDATE posts SET title = :title, link = :link WHERE slug = :slug";
        $params = [
            ':title' => $title,
            ':link' => $link,
            ':slug' => $slug,
        ];
        return $this->db->executeQuery($query, $params);
    }

    public function delete($slug) {
        $query = "DELETE FROM posts WHERE slug = :slug";
        $params = [
            ':slug' => $slug,
        ];
        return $this->db->executeQuery($query, $params);
    }
}
?>

routes. php

<?php
require_once 'db.php';
require_once 'post.php';

session_start();

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header("Location: /login.php");
    exit();
}

// Initialize the database connection
$db = new Database();

// Initialize the Post
$post = new Post($db);

// Handle the request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $slug = trim($_SERVER['REQUEST_URI'], '/');
    $path = explode('/', $slug);
    $slug = $path[0];

    if (count($path) == 2 && $path[1] == 'delete') {
        $post->delete($slug);
        header("Location: /");
        exit();
    } elseif (count($path) == 2 && $path[1] == 'update') {
        $post->update($slug, $_POST['title'], $_POST['link']);
        header("Location: /$slug");
        exit();
    } else {
        $slug = $post->create($_POST['title'], $_POST['link']);
        header("Location: /$slug");
        exit();
    }
} else {
    $slug = trim($_SERVER['REQUEST_URI'], '/');
    if (!empty($slug)) {
        $post_data = $post->getBySlug($slug);
    }
}

// Include the HTML template
require_once 'routes_template.php';
?>

routes_template.php

I Am Unable to Add Routes Template

CREATE is Working

When i Do Update It Is Creating new Post

and DELETE is Completely Not Working

if any body could help?

I would be easier to help if you put your code between ``` and ```` using the </> is the easiest way. :wink:

Example

<?php

$host = 'localhost'; // your database host
$dbname = 'mydatabase'; // your database name
$username = 'myusername'; // your database username
$password = 'mypassword'; // your database password

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // set additional PDO attributes if needed
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

Check Now Sir

i deleted routes_template.php Accidentally

Sponsor our Newsletter | Privacy Policy | Terms of Service