create.php
<form action="/store" method="post">
<input type="text" name="name" placeholder="name">
<input type="text" name="email" placeholder="email">
<input type="file" name="image" placeholder="image">
<button type="submit">Save</button>
</form>
route.php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($_SERVER['REQUEST_URI'] === '/store') {
$controller->store();
}
}
Controller.php
public function store() {
$name = $_POST['name'];
$name = $_POST['name'];
$image = $_POST['image'];
$this->user->create($name, $email, $image);
header('Location: /');
}
User.php
class User {
private $db;
public function __construct() {
$this->db = new PDO('mysql:host=localhost;dbname=php', 'root', '');
}
public function getAll() {
$stmt = $this->db->query('SELECT * FROM users');
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function create($name, $email) {
$stmt = $this->db->prepare('INSERT INTO users (name, email, image) VALUES (:name, :email, :image)');
$stmt->execute(['name' => $name, 'email' => $email, 'image' => $image]);
}