-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
143 lines (128 loc) · 4.97 KB
/
update.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
<!--
Page Update ID
-->
<?php
// import du fichier database.php
require 'database.php';
// on définit par default notre id en null
$id = null;
// on check si on récupère l'id dans notre method get
if (!empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
// si il n'y a pas d'id
if (null == $id) {
// on redirige sur la page index.php
header("Location: index.php");
}
// Si il y a une method post
if (!empty($_POST)) {
// On définit nos variables à envoyer
$nameError = null;
$emailError = null;
$mobileError = null;
// On définit la valeur de nos variable au résultat de la method post
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
// On check nos variable input:
$valid = true;
if (empty($name)) {
$nameError = 'Please enter Name';
$valid = false;
}
if (empty($email)) {
$emailError = 'Please enter Email Address';
$valid = false;
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailError = 'Please enter a valid Email Address';
$valid = false;
}
if (empty($mobile)) {
$mobileError = 'Please enter Mobile Number';
$valid = false;
}
// Si tout est valid alors
if ($valid) {
// update data
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE customers set name = ?, email = ?, mobile =? WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($name, $email, $mobile, $id));
Database::disconnect();
header("Location: index.php");
}
// Sinon on récupère les datas pour les afficher sur la page
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM customers where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$name = $data['name'];
$email = $data['email'];
$mobile = $data['mobile'];
Database::disconnect();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Update a Customer</h3>
</div>
<!-- Notre formulaire d'édition -->
<form class="form-horizontal" action="update.php?id=<?php echo $id ?>" method="post">
<div class="control-group <?php echo !empty($nameError) ? 'error' : ''; ?>">
<label class="control-label">Name</label>
<div class="controls">
<!-- Input pour l'envoi sur la méthod post -->
<input name="name" type="text" placeholder="Name" value="<?php echo !empty($name) ? $name : ''; ?>">
<!-- gestion de l'err no-data -->
<?php if (!empty($nameError)) : ?>
<span class="help-inline"><?php echo $nameError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($emailError) ? 'error' : ''; ?>">
<label class="control-label">Email Address</label>
<div class="controls">
<!-- Input pour l'envoi sur la méthod post -->
<input name="email" type="text" placeholder="Email Address" value="<?php echo !empty($email) ? $email : ''; ?>">
<!-- gestion de l'err no-data -->
<?php if (!empty($emailError)) : ?>
<span class="help-inline"><?php echo $emailError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($mobileError) ? 'error' : ''; ?>">
<label class="control-label">Mobile Number</label>
<div class="controls">
<!-- Input pour l'envoi sur la méthod post -->
<input name="mobile" type="text" placeholder="Mobile Number" value="<?php echo !empty($mobile) ? $mobile : ''; ?>">
<!-- gestion de l'err no-data -->
<?php if (!empty($mobileError)) : ?>
<span class="help-inline"><?php echo $mobileError; ?></span>
<?php endif; ?>
</div>
</div>
<div class="form-actions">
<!-- Submit -->
<button type="submit" class="btn btn-success">Update</button>
<!-- redirect -->
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>