Project Overview
Build a Contact Manager web application that demonstrates all PHP concepts you've learned.
- Form handling with validation and sanitization
- PDO database access with prepared statements
- Sessions for user authentication
- Object-oriented architecture
- CRUD operations (Create, Read, Update, Delete)
- Security best practices throughout
Database and Model
Design the database schema and create a model class for contacts.
Example
<?php
// Database setup
$pdo = new PDO('mysql:host=localhost;dbname=contacts_app', 'root', '', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
class Contact {
public function __construct(
private PDO $db,
public readonly ?int $id = null,
public string $name = '',
public string $email = '',
public string $phone = ''
) {}
public function save(): bool {
if ($this->id) {
$stmt = $this->db->prepare(
'UPDATE contacts SET name=:name, email=:email, phone=:phone WHERE id=:id'
);
return $stmt->execute([
'id' => $this->id, 'name' => $this->name,
'email' => $this->email, 'phone' => $this->phone
]);
}
$stmt = $this->db->prepare(
'INSERT INTO contacts (name, email, phone) VALUES (:name, :email, :phone)'
);
return $stmt->execute([
'name' => $this->name, 'email' => $this->email,
'phone' => $this->phone
]);
}
public static function findAll(PDO $db): array {
return $db->query('SELECT * FROM contacts ORDER BY name')->fetchAll();
}
public function delete(): bool {
$stmt = $this->db->prepare('DELETE FROM contacts WHERE id = :id');
return $stmt->execute(['id' => $this->id]);
}
}
?> 