diff --git a/assets/php/Database.php b/assets/php/Database.php new file mode 100644 index 0000000000000000000000000000000000000000..7f3669c3174e1b44c2db500e76350b1bbd20833c --- /dev/null +++ b/assets/php/Database.php @@ -0,0 +1,15 @@ +<?php + +class Database { + protected PDO $db; + + public function __construct() + { + $this->db = new PDO('sqlite:' . __DIR__ . '/db.sqlite'); + $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); + } + + +} + diff --git a/assets/php/Recipe.php b/assets/php/Recipe.php new file mode 100644 index 0000000000000000000000000000000000000000..d35f3126277e761cd4f7a29314018bfbb521ee23 --- /dev/null +++ b/assets/php/Recipe.php @@ -0,0 +1,29 @@ +<?php + +require_once 'Database.php'; + +class Recipe extends Database +{ + public function __construct() + { + parent::__construct(); + + $this->db->exec('CREATE TABLE IF NOT EXISTS recipe ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title VARCHAR(100) NOT NULL, + description TEXT NOT NULL, + notation INTEGER, + thumbnail VARCHAR(50) NOT NULL +)'); + } + + public function save(string $title, string $description): void + { + $statement = $this->db->prepare("INSERT INTO recipe ('title', 'description', 'thumbnail') VALUES (:title, :description, 'photo.jpg')"); + + $statement->bindValue(':title', $title); + $statement->bindValue(':description', $description); + + $statement->execute(); + } +} \ No newline at end of file diff --git a/assets/php/db.sqlite b/assets/php/db.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..7441652bea712336b34f325339dfe3a68b23cf33 Binary files /dev/null and b/assets/php/db.sqlite differ diff --git a/assets/php/saveRecipe.php b/assets/php/saveRecipe.php new file mode 100644 index 0000000000000000000000000000000000000000..6107dc1b8d3cefd855de7215d2ab6787d856b333 --- /dev/null +++ b/assets/php/saveRecipe.php @@ -0,0 +1,16 @@ +<?php + +require_once 'Recipe.php'; + +$title = htmlspecialchars($_POST['title']); +$description = htmlspecialchars($_POST['description']); + +if (strlen($title) > 100 || $title === '' || $description == '') { + header('Location: /create.php'); + exit(); +} + +$recipe = new Recipe(); + +$recipe->save($title, $description); +header('Location: /create.php'); \ No newline at end of file diff --git a/create.php b/create.php index b3d9bbc7f3711e882119cd6b3af051245d859d04..56166d3fb8b09c588ba720eebabb59ddfdb24a8c 100644 --- a/create.php +++ b/create.php @@ -1 +1,47 @@ +<!doctype html> +<html lang="fr"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Créer une recette</title> + <style> + .sr-only { + border: 0 !important; + clip: rect(1px, 1px, 1px, 1px) !important; + -webkit-clip-path: inset(50%) !important; + clip-path: inset(50%) !important; + height: 1px !important; + overflow: hidden !important; + padding: 0 !important; + position: absolute !important; + width: 1px !important; + white-space: nowrap !important; + } + </style> +</head> +<body> +<form action="assets/php/saveRecipe.php" method="post"> + <label for="title">Titre</label> + <input name="title" type="text" placeholder="Titre" id="title"/> + + <label for="description">Description</label> + <textarea name="description" id="description" cols="30" rows="10"></textarea> + + <button type="submit">Enregistrer</button> +</form> + <?php +$pdo = new PDO('sqlite:'. __DIR__. '/assets/php/db.sqlite'); +$entries = $pdo->query('SELECT * FROM recipe')->fetchAll(); +?> +<ul> +<?php +foreach ($entries as $entry) { + ?> +<li><?php echo $entry['description']?></li> +<?php +} +?> + </ul> +</body> +</html> \ No newline at end of file