From 280421efba2e2799a2834e21e176b7c3a9a6323f Mon Sep 17 00:00:00 2001 From: Giildo <giildo.jm@gmail.com> Date: Wed, 26 Feb 2025 17:27:15 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Create=20form=20post?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/php/Database.php | 15 +++++++++++++ assets/php/Recipe.php | 29 ++++++++++++++++++++++++ assets/php/db.sqlite | Bin 0 -> 12288 bytes assets/php/saveRecipe.php | 16 +++++++++++++ create.php | 46 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 assets/php/Database.php create mode 100644 assets/php/Recipe.php create mode 100644 assets/php/db.sqlite create mode 100644 assets/php/saveRecipe.php diff --git a/assets/php/Database.php b/assets/php/Database.php new file mode 100644 index 0000000..7f3669c --- /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 0000000..d35f312 --- /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 GIT binary patch literal 12288 zcmeI&u}<4S7zgk>hdNTE;xa{<asnxe3rZ=dlM%v2R*4!6HKuK*$~f2H>f&>pi&$7t z->c8jt&h;9570|&LZAanmsbBzv6HjE`#bt=UzD?Vy+m>LS>~fiv0d_vP)ZIMBZO4- zQPIb3QT<`%cA?*A>&L1}Ix&5!yNuQE#ConzC<s6R0uX=z1Rwwb2tWV=5P-m66gV}^ z`sOC>tLQ@T$yn$i_tS)rr#y`xE-QzD=Y}2&-Pb*jJ?L$x(TH#9o%YQct=1b2`jyTz za~>xdFV4ojgyI@&*O;zkz`B0uz3~Ee8g!4{pwHfVedeBrCtY7}JNEo=Yu>36B{+NU z28Ty((0;kSZ8QHQWd3=tS2PUxB+ipeB{F59_fg-vcRQ6Tn%^s*SEgQ0M;B?7h(+?N zrQn)9o6XJDpTzoMeY38u4a?9A6a*ju0SG_<0uX=z1Rwwb2teT97FaV)Vp*iQvy)v) zB|BfTVe^Se>Kmk~=kJ^^&De{E4=Yttzga6gT0%L+9Nro#Czk0%3f>WN*lvxdQaMLl z2+6KwE(XN~+nH^7akpm?d{EI(=4I|)(XhXqxV{v&ZaaHr$o?XsY5eisRi=L*xND$~ sG|c9IYW*Vmg@OPCAOHafKmY;|fB*y_009U<;6D<m(Mp3B|57l21M>334FCWD literal 0 HcmV?d00001 diff --git a/assets/php/saveRecipe.php b/assets/php/saveRecipe.php new file mode 100644 index 0000000..6107dc1 --- /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 b3d9bbc..56166d3 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 -- GitLab