-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
180 lines (140 loc) · 6.37 KB
/
edit.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
session_start();
if (!isset($_SESSION["admin"])) {
header("Location: index.php");
return;
}
require "database.php";
$id = $_GET["id"];
$statement = $conn->prepare("SELECT * FROM articles WHERE id = :id LIMIT 1");
$statement->execute([":id" => $id]);
if ($statement->rowCount() == 0) {
http_response_code(404);
echo ("HTTP 404 NOT FOUND");
return;
}
$article = $statement->fetch(PDO::FETCH_ASSOC);
$error = null;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["title"]) || empty($_POST["publish_date"]) || empty($_POST["information"])) {
$error = "Porfavor rellene todos los espacios.";
} else if ($_FILES["picture"]["error"] == 4 ) {
$statement = $conn->prepare("UPDATE articles SET title = :title, subtitle = :subtitle, publish_date = :publish_date, information = :information,
author = :author, origin = :origin, links = :links WHERE id = :id");
$statement->execute([
":id" => $id,
":title" => $_POST["title"],
":subtitle" => $_POST["subtitle"],
":publish_date" => $_POST["publish_date"],
":information" => $_POST["information"],
":author" => $_POST["author"],
":origin" => $_POST["origin"],
":links" => $_POST["links"],
]);
$_SESSION["flash"] = ["message" => "Artículo {$_POST['title']} actualizado."];
header("Location: panelAdmins.php");
return;
} else {
$fecha = new DateTime();
$picture = $_FILES['picture']['name'] . $fecha->getTimestamp();
$path = $_FILES['picture']['tmp_name'];
$destiny = "images/" . $picture;
move_uploaded_file($path, $destiny);
$statement = $conn->prepare("UPDATE articles SET title = :title, subtitle = :subtitle, publish_date = :publish_date, information = :information,
author = :author, picture = :destiny, origin = :origin, links = :links WHERE id = :id");
$statement->execute([
":id" => $id,
":title" => $_POST["title"],
":subtitle" => $_POST["subtitle"],
":publish_date" => $_POST["publish_date"],
":information" => $_POST["information"],
":author" => $_POST["author"],
":destiny" => $destiny,
":origin" => $_POST["origin"],
":links" => $_POST["links"],
]);
$_SESSION["flash"] = ["message" => "Artículo {$_POST['title']} actualizado."];
header("Location: panelAdmins.php");
return;
}
}
?>
<?php require "partials/header.php" ?>
<div class="container pt-5">
<div class="row justify-content-center">
<div class="col-md-8 mt-5">
<div class="card">
<div class="card-header">Editar articulo</div>
<div class="card-body">
<?php if ($error) : ?>
<p class="text-danger">
<?= $error ?>
<?php endif ?>
<form method="POST" enctype="multipart/form-data" action="edit.php?id=<?= $article["id"] ?>">
<div class="mb-3 row">
<label for="title" class="col-md-4 col-form-label text-md-end">Título</label>
<div class="col-md-6">
<input value="<?= $article["title"] ?>" id="title" type="text" class="form-control" name="title" autocomplete="title" autofocus>
</div>
</div>
<div class="mb-3 row">
<label for="subtitle" class="col-md-4 col-form-label text-md-end">Subtitulo</label>
<div class="col-md-6">
<input value="<?= $article["subtitle"] ?>" id="subtitle" type="text" class="form-control" name="subtitle" autocomplete="subtitle" autofocus>
</div>
</div>
<div class="mb-3 row">
<label for="picture" class="col-md-4 col-form-label text-md-end">Imagen</label>
<div class="col-md-6">
<input type="file" class="form-control" name="picture" placeholder="Imagen" autofocus>
</div>
</div>
<div class="mb-3 row">
<label for="author" class="col-md-4 col-form-label text-md-end">Autor</label>
<div class="col-md-6">
<input value="<?= $article["author"] ?>" id="author" type="text" class="form-control" name="author" autocomplete="author" autofocus>
</div>
</div>
<div class="mb-3 row">
<label for="origin" class="col-md-4 col-form-label text-md-end">Origen</label>
<div class="col-md-6">
<input value="<?= $article["origin"] ?>" id="origin" type="text" class="form-control" name="origin" autocomplete="origin" autofocus>
</div>
</div>
<div class="mb-3 row">
<label for="category" class="col-md-4 col-form-label text-md-end">Categoría</label>
<div class="col-md-6">
<input value="<?= $article["category"] ?>" id="category" type="text" class="form-control" name="category" autocomplete="category" autofocus>
</div>
</div>
<div class="mb-3 row">
<label for="publish_date" class="col-md-4 col-form-label text-md-end">Fecha de publicación</label>
<div class="col-md-6">
<input value="<?= $article["publish_date"] ?>" id="publish_date" type="text" class="form-control" name="publish_date" autocomplete="date" placeholder="YYYY-MM-DD" autofocus>
</div>
</div>
<div class="mb-3 row">
<label for="information" class="col-md-4 col-form-label text-md-end">Información</label>
<div class="col-md-6">
<textarea id="information" type="text" class="form-control" name="information" autocomplete="information" autofocus><?= $article["information"] ?></textarea>
</div>
</div>
<div class="mb-3 row">
<label for="links" class="col-md-4 col-form-label text-md-end">Links</label>
<div class="col-md-6">
<input value="<?= $article["links"] ?>" id="links" type="text" class="form-control" name="links" autofocus>
</div>
</div>
<div class="mb-3 row">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">Editar</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>