-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
237 lines (216 loc) · 7.62 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
//Config BDD WAMP
$mysqlDsn = 'mysql:dbname=cinema;host=localhost:8889';
$username = 'root';
$password = 'root';
//Connexion BDD
try {
$obj_pdp = new PDO($mysqlDsn,$username,$password);
echo 'DB connected<br>';
}catch (PDOException $e) {
echo 'Erreur de connexion : ' . $e->getMessage();
}
//Mise en place des tables de la BDD
try {
$obj_pdp = new PDO($mysqlDsn,$username,$password);
$obj_pdp->query(
'CREATE TABLE admin
(
id_admin INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(254) NOT NULL UNIQUE,
password VARCHAR(60) NOT NULL,
role VARCHAR (254) NOT NULL
) ENGINE=InnoDB;
CREATE TABLE users
(
id_users INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(254) NOT NULL UNIQUE,
password VARCHAR(60) NOT NULL,
first_name VARCHAR(254) NOT NULL,
last_name VARCHAR(254) NOT NULL,
birthday DATETIME NOT NULL,
role VARCHAR (254) NOT NULL
) ENGINE=InnoDB;
CREATE TABLE films
(
id_films INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(254) NOT NULL,
release_date DATE NOT NULL,
score FLOAT NOT NULL
) ENGINE=InnoDB;
CREATE TABLE shows
(
id_shows INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
cinema_seats TINYINT NULL,
time_slot DATETIME NOT NULL,
room_number TINYINT NOT NULL,
select_film INT(11) NOT NULL,
select_user INT(11) NOT NULL,
FOREIGN KEY (select_film) REFERENCES films(id_films),
FOREIGN KEY (select_user) REFERENCES users(id_users)
) ENGINE=InnoDB;
');
echo 'Toutes les tables sont créées <br>';
}catch (PDOException $e) {
echo 'Impossible de créer les tables : ' . $e->getMessage();
}
//Insertion d'un user dans la BDD
try{$obj_pdo = new PDO($mysqlDsn,$username,$password);
$statement = $obj_pdo->prepare('INSERT INTO users (email, password, first_name, last_name, role ,birthday)
VALUES (:email, :password, :first_name, :last_name, :role ,:birthday)');
$statement->bindValue(':email', 'john.doe@example.com');
// Hash du mot de passe en utilisant BCRYPT
$statement->bindValue(':password', password_hash('test_p4ssword', PASSWORD_BCRYPT));
$statement->bindValue(':first_name', 'john');
$statement->bindValue(':last_name', 'doe');
$statement->bindValue(':role', 'ROLE_USER');
$statement->bindValue(':birthday', '1996-04-18');
if ($statement->execute()) {
echo 'Utilisateur créé <br>';
} else {
$errorInfo = $statement->errorInfo();
echo 'SQLSTATE : '.$errorInfo[0].'<br>';
echo 'Erreur du driver : '.$errorInfo[1].'<br>';
echo 'Message : '.$errorInfo[2]. '<br>';
}
} catch (PDOException $e){
echo 'Impossible de créer l\'utilisateur :' . $e->getMessage();
}
//Insertion d'un film dans la bdd
try{
$obj_pdp = new PDO($mysqlDsn,$username,$password);
$obj_pdp->query('INSERT INTO films (name, release_date, score)
VALUES ("Star Wars : L\'Empire contre attaque", "1980-08-20", "4.8")');
echo 'Film crée <br>';
}catch (PDOException $e){
echo 'Impossible de créer le film' . $e->getMessage();
}
//Connexion à l'utilisateur et vérification des droits
try {
$obj_pdo = new PDO($mysqlDsn, $username, $password);
$statement = $obj_pdo->prepare('SELECT * FROM users WHERE email = :email');
$statement->bindValue(':email', 'john.doe@example.com');
if ($statement->execute()) {
$user = $statement->fetch(PDO::FETCH_ASSOC);
if ($user === false) {
echo 'Identifiants invalides';
} else {
if (password_verify('test_p4ssword', $user['password'])) {
if ($user['role'] === 'ROLE_ADMIN'){
echo 'Bienvenue Admin ' . $user['first_name'] . '<br>';
} else{
echo 'Bienvenue ' . $user['first_name'] . '<br>';
}
} else {
echo 'Identifiants invalides';
}
}
} else {
echo 'Impossible de récupérer l\'utilisateur';
}
}catch (PDOException $e){
echo 'Impossible de se connecter à l\'utilisateur';
}
//Insertion d'une réservation dans la BDD
try {
$obj_pdo = new PDO($mysqlDsn, $username, $password);
$statement = $obj_pdo->prepare("INSERT INTO shows
(cinema_seats, time_slot, room_number, select_film, select_user)
VALUES
('3','2019-08-20 19:00:00','4', '1', '1');");
if ($statement->execute()) {
echo 'Réservation créé <br>';
} else {
$errorInfo = $statement->errorInfo();
echo 'SQLSTATE : ' . $errorInfo[0] . '<br>';
echo 'Erreur du driver : ' . $errorInfo[1] . '<br>';
echo 'Message : ' . $errorInfo[2] . '<br>';
}
}
catch (PDOException $e){
echo 'Impossible de créer la réservation' . $e->getMessage();
}
//Selection et affichage d'une réservation
try {
$obj_pdo = new PDO($mysqlDsn, $username, $password);
$statement = $obj_pdo->prepare("SELECT cinema_seats as seats, time_slot, room_number,
name as film_name, first_name, last_name, email
FROM shows
INNER JOIN films ON shows.select_film = films.id_films
INNER JOIN users ON shows.select_user = users.id_users
WHERE id_shows like :id;");
$statement->bindValue(':id', '1');
if ($statement->execute()) {
while ($shows = $statement->fetch(PDO::FETCH_OBJ)) {
echo 'La reservation de ' . $shows->first_name . ' ' .$shows->last_name .
'(' .$shows->email . ')' . ' : ' . $shows->film_name . '<br>' .
$shows->time_slot . '<br>Nombre de place : ' . $shows->seats .
'<br>Salle n°' . $shows->room_number . '<br>';
}
} else {
$errorInfo = $statement->errorInfo();
echo 'SQLSTATE : ' . $errorInfo[0] . '<br>';
echo 'Erreur du driver : ' . $errorInfo[1] . '<br>';
echo 'Message : ' . $errorInfo[2] . '<br>';
}
}catch (PDOException $e){
echo 'Impossible de sélectionner la reservation';
}
//Modification de la réservation
try {
$obj_pdo = new PDO($mysqlDsn, $username, $password);
$statement = $obj_pdo->prepare("UPDATE shows
SET
cinema_seats = '6'
WHERE
id_shows = '1';");
if ($statement->execute()) {
echo 'Reservation modifiée<br>';
} else {
$errorInfo = $statement->errorInfo();
echo 'SQLSTATE : ' . $errorInfo[0] . '<br>';
echo 'Erreur du driver : ' . $errorInfo[1] . '<br>';
echo 'Message : ' . $errorInfo[2] . '<br>';
}
}catch (PDOException $e){
echo 'Impossible de modifier la reservation';
}
//Suppression de la réservation
try {
$obj_pdo = new PDO($mysqlDsn, $username, $password);
$statement = $obj_pdo->prepare("DELETE FROM shows
WHERE
id_shows = '1';");
if ($statement->execute()) {
echo 'Reservation supprimée<br>';
} else {
$errorInfo = $statement->errorInfo();
echo 'SQLSTATE : ' . $errorInfo[0] . '<br>';
echo 'Erreur du driver : ' . $errorInfo[1] . '<br>';
echo 'Message : ' . $errorInfo[2] . '<br>';
}
}catch (PDOException $e){
echo 'Impossible de supprimer la reservation';
}
if(isset($_POST['email'])){
$email = $_POST['email'];
$obj_pdo = new PDO($mysqlDsn, $username, $password);
$sql = 'SELECT * FROM users WHERE email = :email';
$statement = $obj_pdo->prepare($sql);
$statement->bindValue(':email', $email, PDO::PARAM_STR);
$results = $statement->execute();
if ($results){
$user = $statement->fetch();
if($user){
echo 'Utilisateur déconnecté';
}
}
echo '<br>' . $sql;
}
?>
<form method="POST" action="index.php">
<div class="form-input">
<input type="email" name="email" placeholder="Entrez votre email">
</div>
<input type="submit" name="submit" value="Logout">
</form>