-
Notifications
You must be signed in to change notification settings - Fork 0
/
createEditStore.php
158 lines (138 loc) Β· 5.13 KB
/
createEditStore.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
<?php include_once('createEditStoreHeader.html');
include_once('db.php'); // because one does not simply declare class several times
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_GET['createSuccess']) && $_GET['createSuccess'] == 1){
?>
<div class="alert alert-success"><?php echo 'Store successfully created'; ?> </div>
<?php }
elseif(isset($_GET['updateSuccess']) && $_GET['updateSuccess'] == 1) {
?>
<div class="alert alert-success"><?php echo 'Store successfully updated'; ?> </div>
<?php }
// inserting into the Stores table
// let's make Country and City parameters selectable options using values from the database
// address need validation, whether a city is located in a selected country. This can be done using SQL function (as Bakur says at least)
$db = DB::getConnection();
$st2h = $db->prepare("SELECT Countries.name AS country, Countries.id AS country_id, Cities.name AS city, Cities.id AS city_id
FROM Countries JOIN Cities
ON Countries.id=Cities.country_id");
$st2h->execute();
$addresses = $st2h->fetchAll(PDO::FETCH_ASSOC);
if($_POST != null)
{
$store = $_POST;
if(isset($store['create']))
{
$sth = $db->prepare("INSERT INTO Stores(name, country_id, city_id)
VALUES (?, ?, ?)");
$sth->execute([$store['name'], $store['country'], $store['city']]);
header('Location: ' . $_SERVER['PHP_SELF']. '?createSuccess=1');
}
elseif(isset($_POST['update']))
{
$sth = $db->prepare("UPDATE Stores
SET name=?, country_id=?, city_id=?
WHERE id=?");
$sth->execute([$store['name'], $store['country'], $store['city'], $store['id']]);
header('Location: ' . $_SERVER['PHP_SELF']. '?id=' . $store['id'] . '&updateSuccess=1');
}
die;
}
if(isset($_GET['id']))
{
$id = $_GET['id'];
if(filter_var($id, FILTER_VALIDATE_INT) && $id>0)
{
$id = $id;
$sth = $db->prepare("SELECT Stores.id AS id, Stores.name AS name, Countries.name AS country, Cities.name AS city
FROM Stores JOIN Countries
ON Stores.country_id=Countries.id
JOIN Cities
ON Stores.city_id=Cities.id
WHERE Stores.id=?");
//$sth->bindParam(':id', $id);
$sth->execute([$id]);
$store = $sth->fetch();
?>
<h1>Edit a Store</h1>
<form action="createEditStore.php" method="post" class="container">
<div class="form-group">
<label for="name">Name</label>
<input id="name" class="form-control" name="name" autocomplete="off" required="required" value="<?php echo $store['name']; ?>">
</div>
<div class="form-group">
<label for="country">Country:</label>
<select id="country" name="country" class="form-control" required="required">
<?php
foreach($addresses as $address)
{
?>
<option value="<?php echo $address['country_id']; ?>" <?php if($store['city'] == $address['city']){ ?> selected="selected" <?php } ?> >
<?php echo $address['country']; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="city">City:</label>
<select id="city" name="city" class="form-control" required="required">
<?php
foreach($addresses as $address)
{
?>
<option value="<?php echo $address['city_id']; ?>" <?php if($store['city'] == $address['city']){ ?> selected="selected" <?php } ?> >
<?php echo $address['city']; ?></option>
<?php } ?>
</select>
</div>
<input type="hidden" name="id" value="<?php echo $store['id']; ?>">
<input type="hidden" name="update" value="1">
<button type="submit" class="btn btn-default">Edit</button>
<button type="reset" class="btn btn-default">Reset</button>
</form>
<?php }
}
else
{
?>
<h1>Create a Store</h1>
<form action="createEditStore.php" method="post" class="container">
<div class="form-group">
<label for="name">Name</label>
<input id="name" class="form-control" name="name" autocomplete="off" required="required">
</div>
<div class="form-group">
<label for="country">Country:</label>
<select id="country" name="country" class="form-control" required="required">
<?php
foreach($addresses as $address)
{
?>
<option value="<?php echo $address['country_id']; ?>"><?php echo $address['country']; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="city">City:</label>
<select id="city" name="city" class="form-control" required="required">
<?php
foreach($addresses as $address)
{
?>
<option value="<?php echo $address['city_id']; ?>"><?php echo $address['city']; ?></option>
<?php } ?>
</select>
</div>
<input type="hidden" name="create" value="1">
<button type="submit" class="btn btn-default">Create</button>
<button type="reset" class="btn btn-default">Reset</button>
</form>
<?php } ?>
<footer><a href="index.php">Go home</a></footer>
<?php include('footer.php'); ?>
π π π π
π π π π
π π π
π π π π
π π π πο»Ώ