-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.php
68 lines (60 loc) · 2.01 KB
/
create.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
$forAuth = true;
include 'includes/header.php';
$errors = [];
if(isset($_POST['submit'])) {
$title = trim($_POST['title']);
$description = trim($_POST['description']);
$content = trim($_POST['content']);
$image = $_FILES['image'];
$errors = validatePost($title, $description, $content, $image);
if(count($errors) === 0) {
$imagePath = uploadImage($image);
$query = $db->prepare("INSERT INTO posts (title, description, content, user_id, image_path) VALUES (:title, :description, :content, :user_id, :image_path)");
$query->execute([
'title' => $title,
'description' => $description,
'content' => $content,
'user_id' => $user['id'],
'image_path' => $imagePath,
]);
redirect('index.php');
}
}
?>
<h1>Create post</h1>
<form action="create.php" novalidate method="post" enctype="multipart/form-data">
<div>
<label>
Post title:<br>
<input type="text" placeholder="Post title" name="title" value="<?= $title ?? '' ?>">
<?= $errors['title'] ?? '' ?>
</label>
</div>
<div>
<label>
Post description:<br>
<textarea placeholder="Post description" name="description"><?= $description ?? '' ?></textarea>
<?= $errors['description'] ?? '' ?>
</label>
</div>
<div>
<label>
Post content:<br>
<textarea placeholder="Post content" name="content"><?= $content ?? '' ?></textarea>
<?= $errors['content'] ?? '' ?>
</label>
</div>
<div>
<label>
Post image:<br>
<input type="file" name="image">
<?= $errors['image'] ?? '' ?>
</label>
</div>
<div>
<input type="submit" value="Create" name="submit">
</div>
</form>
<?php
include 'includes/footer.php';