-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_controller.php
86 lines (75 loc) · 2.29 KB
/
create_controller.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
<?php
include "connect_to_db.php";
include "view/header.php";
session_start();
?>
<!-- Fonction de triage des données réçus par le foirmulaire -->
<?php
function triage($data){
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!-- Validation -->
<?php
if(isset($_POST['create_submit'])):
if(empty($_POST['nom'])):
$_SESSION['nameError'] = 'Name is required';
else:
$_SESSION['nameError'] = null;
endif;
if(empty($_POST['possesseur'])):
$_SESSION['possesseurError'] = 'Possesseur is required';
else:
$_SESSION['possesseurError'] = null;
endif;
if(empty($_POST['console'])):
$_SESSION['consoleError'] = 'Console is require';
else:
$_SESSION['consoleError'] = null;
endif;
if(empty($_POST['prix'])):
$_SESSION['prixError'] = 'Prix is required';
elseif( (int)$_POST['prix'] < 1 ):
$_SESSION['prixError'] = 'Prix must be over than 0';
else:
$_SESSION['prixError'] = null;
endif;
if(empty($_POST['nbre_joueurs_max'])):
$_SESSION['nbmaxError'] = 'Nombre de joueur is required';
elseif( (int)$_POST['nbre_joueurs_max'] < 1):
$_SESSION['nbmaxError'] = 'Nombre de jour must over than 0';
else:
$_SESSION['nbmaxError'] = null;
endif;
if(empty($_POST['commentaires'])):
$_SESSION['commentaireError'] = 'Commentaire is require';
else:
$_SESSION['commentaireError'] = null;
endif;
endif;
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["create_submit"])):
$nom = triage($_POST['nom']);
$possesseur = triage($_POST['possesseur']);
$console = triage($_POST['console']);
$prix = triage($_POST['prix']);
$nbreMax = triage($_POST['nbre_joueurs_max']);
$commentaires = triage($_POST['commentaires']);
/**
* Insert all data from post into databse
*/
$insertdata = $db->prepare('INSERT INTO jeux_video(nom, possesseur, console, prix, nbre_joueurs_max, commentaires) VALUES(:nom, :possesseur, :console, :prix, :nbre_joueurs_max, :commentaires)');
$insertdata->execute(array(
'nom' => $nom,
'possesseur' => $possesseur,
'console' => $console,
'prix' => $prix,
'nbre_joueurs_max' => $nbreMax,
'commentaires' => $commentaires
));
$_SESSION['success'] = "Le jeu a été bien ajouté";
endif;
header('Location: create_view.php');
?>