-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.php
More file actions
65 lines (58 loc) · 2.63 KB
/
edit.php
File metadata and controls
65 lines (58 loc) · 2.63 KB
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
<?php
if( empty($_GET['id']) || !is_numeric($_GET['id']) ) {
header('Location: /404.php');
exit;
}
require 'inc/functions.php';
require 'inc/pdo.php';
$query = $pdo -> prepare('SELECT * FROM city WHERE ID=:id');
$query -> bindValue(':id', $_GET['id'], PDO::PARAM_INT);
$query -> execute();
$city = $query->fetch();
if(!$city) {
header('Location: /404.php');
exit;
}
$errors = [];
if(!empty($_POST['submitted'])) {
$name = secureTextByArray($_POST, 'name');
$district = secureTextByArray($_POST, 'district');
$countryCode = secureTextByArray($_POST, 'countryCode');
$population = getValueByArray($_POST, 'population');
checkLengthTextValid($name, 1, 35, $errors, 'name');
checkLengthTextValid($district, 1, 20, $errors, 'district');
checkLengthTextValid($countryCode, 3, 3, $errors, 'countryCode');
checkNumericValid($population, $errors, 'population', 0, 99_999_999_999);
if( empty($errors) ) {
$query = $pdo -> prepare('UPDATE city SET Name=:name,CountryCode=:countryCode,District=:district,Population=:population WHERE ID=:id');
$query->bindValue(':id', $_GET['id'], PDO::PARAM_INT);
$query->bindValue(':name', $name);
$query->bindValue(':countryCode', $countryCode);
$query->bindValue(':district', $district);
$query->bindValue(':population', $population, PDO::PARAM_INT);
$success = $query->execute();
}
}
$title = 'Modifier';
include 'inc/header.php';
?>
<div class="container">
<?php if(isset($success) && $success) { ?>
<div class="success">
La ville a bien été modifier ! <a href="/city.php?id=<?=$_GET['id']?>">Voir la ville</a>
</div>
<?php } else { ?>
<h1>Nouvelle Ville</h1>
<form action="" method="post">
<?php
buildInput(getValueByArray($_POST, 'name', $city['Name']), 'Nom *', 'text', 'name', $errors);
buildInput(getValueByArray($_POST, 'district', $city['District']), 'District *', 'text', 'district', $errors);
buildInput(getValueByArray($_POST, 'countryCode', $city['CountryCode']), 'Country Code *', 'text', 'countryCode', $errors);
buildInput(getValueByArray($_POST, 'population', $city['Population']), 'Population *', 'number', 'population', $errors);
?>
<input type="submit" name="submitted" value="Enregistrer">
</form>
<?php } ?>
</div>
<?php
include 'inc/footer.php';