PHP

Building a CRUD Application with PHP & MySQL -2-Test

Learn how to build a complete CRUD application using PHP and MySQL with proper validation and security.

PHPMySQLCRUDBackend

Thumbnail for Building a CRUD Application with PHP & MySQL -2-Test

Overview

This project demonstrates how to build a full CRUD (Create, Read, Update, Delete) application using PHP and MySQL.

Database Setup

sql
CREATE TABLE tasks (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  description TEXT,
  status ENUM('pending', 'completed') DEFAULT 'pending',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

PHP Connection

php
<?php
class Database {
    private $host = 'localhost';
    private $db   = 'task_manager';
    private $user = 'root';
    private $pass = '';

    public function connect(): PDO {
        $dsn = "mysql:host={$this->host};dbname={$this->db}";
        return new PDO($dsn, $this->user, $this->pass, [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ]);
    }
}
?>

Create Task

php
<?php
function createTask(PDO $pdo, string $title, string $desc): bool {
    $stmt = $pdo->prepare("INSERT INTO tasks (title, description) VALUES (?, ?)");
    return $stmt->execute([$title, $desc]);
}
?>

This approach uses prepared statements to prevent SQL injection attacks.

Related Projects

Comments (0)

Leave a Comment

No comments yet. Be the first to comment!