-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-store.php
50 lines (40 loc) · 1.38 KB
/
post-store.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
require "../src/Validator.php";
if (!Validator::required($_POST['title'])) {
$errors['title'] = "Titel mag niet leeg zijn";
}
$db = new Database();
// in $errors worden de foutmeldingen opgeslagen
$errors = [];
if ($_POST != null) {
// spaties voor en na de invoer weghalen
$_POST['title'] = trim($_POST['title']);
// controleren of de titel is ingevuld
if (strlen($_POST['title']) == 0) {
$errors['title'] = "Titel mag niet leeg zijn";
}
if (strlen($_POST['title']) > 255) {
$errors['title'] = "Titel mag niet langer zijn dan 255 tekens";
}
// spaties voor en na de invoer weghalen
$_POST['content'] = trim($_POST['content']);
if (strlen($_POST['content']) == 0) {
$errors['content'] = "De content mag niet leeg zijn";
}
if (empty($errors)) {
// invoeren van de gegevens in de database
$db->query("INSERT INTO posts (title, content, user_id) VALUES (?, ?, ?)", [
$_POST['title'],
$_POST['content'],
63, // user_id hard coded wordt later vervangen door
]);
//hier kan je alleen komen als de query goed is uitgevoerd
flash("Post is opgeslagen", true, 3000);
// doorsturen naar posts pagina
header("Location: /posts");
}
}
//er is geen post dus we laten het formulier zien
view("post-create", [
'errors' => $errors
]);