-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimal-edit.php
199 lines (161 loc) · 10.3 KB
/
animal-edit.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
<?php
// check to make sure the id parameter is specified in the URL
if (isset($_GET['id'])){
$_SESSION['animal_id'] = $_GET['id'];
// prepare statement and execute, prevents SQL injection
$stmt = $pdo->prepare('SELECT * FROM animals a LEFT JOIN photos p ON a.idanimals = p.fk_idanimals
INNER JOIN breeds b ON a.fk_idbreeds = b.idbreeds
INNER JOIN animal_types aty ON b.fk_idanimal_types = aty.idanimal_types
INNER JOIN sex s ON a.fk_idsex = s.idsex
LEFT JOIN pregnancies prg ON a.fk_idpregnancies = prg.idpregnancies
LEFT JOIN users u ON a.fk_idusers = u.idusers
INNER JOIN health h ON a.fk_idhealth = h.idhealth WHERE a.idanimals = ?');
$stmt->execute([$_GET['id']]);
// fetch from database, return as an array
$animal = $stmt->fetch(PDO::FETCH_ASSOC);
// check if product exist (array not empty)
if (!$animal){
// Simple error ro display id the id doen't exist/ is empty
die('Animal does not exist!');
}
} else {
// simple error if ID wasn't specified
die('Animal ID was not specified!');
}
?>
<?=template_header("Žival")?>
<div class="row">
<div class="col-12 col-sm-7 col-md-6 col-lg-4 col-xl-5 animal">
<img src="./<?=$animal['url']?>" alt="<?=$animal['url']?>" style="max-width:350px; height:auto;">
</div>
<div class="col-12 col-sm-5 col-md-6 col-lg-8 col-xl-7 animal-info">
<h1><?=$animal['ear_tag']?> <?=$animal['name']?></h1>
<div class="description">
<div class="insert-form my-form">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card user-form">
<div class="card-header">
<h1 class="card-title">Podatki živali</h1>
</div>
<div class="card-body">
<form method="POST" action="./inc/animal-insert.inc.php" class="content" enctype="multipart/form-data">
<div class="form-group">
<label>Lastnik:</label>
<?php $querry = "SELECT * FROM users WHERE idusers ='".$animal['fk_idusers']."' ";
$stmt = $pdo->prepare($querry);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>
<?php foreach($users as $user): ?>
<input list="owners" type="text" name="owner" id="owner" value="<?=$user['username']?>">
<?php endforeach; ?>
<?php $query = "SELECT * FROM users";
$stmt = $pdo->prepare($query);
$stmt->execute();
$owners = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>
<datalist id="owners">
<?php foreach ($owners as $owner): ?>
<option value="<?=$owner['fname']?> <?=$owner['lname']?>; <?=$owner['username']?>">
<?php endforeach; ?>
</datalist>
</div>
<div class="form-group">
<label>Rojstvo:</label>
<input type="date" name="birth" id="birth" value="<?=$animal['birth']?>" disabled>
<label for="breed">Pasma:</label>
<input type="text" name="breed" id="breed" value="<?=$animal['type']?>: <?=$animal['breed']?>" disabled>
<label for="sex">Spol:</label>
<input type="text" name="sex" id="sex" value="<?=$animal['sex']?>" disabled>
</div>
<div class="form-group">
<label for="mother">Mati:</label>
<?php $querry = "SELECT * FROM animals a WHERE idanimals ='".$animal['idmother']."' ";
$stmt = $pdo->prepare($querry);
$stmt->execute();
$mothers = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>
<?php foreach($mothers as $mother): ?>
<input type="text" name="mother" id="mother" value="<?=$mother['ear_tag']?>: <?=$mother['name']?>" disabled>
<?php endforeach; ?>
</div>
<div class="form-group">
<label for="father">Oče:</label>
<?php $querry = "SELECT * FROM animals a WHERE idanimals ='".$animal['idfather']."' ";
$stmt = $pdo->prepare($querry);
$stmt->execute();
$fathers = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>
<?php foreach($fathers as $father): ?>
<input type="text" name="father" id="father" value="<?=$father['ear_tag']?>: <?=$father['name']?>" disabled>
<?php endforeach; ?>
</div>
<div class="form-group">
<label for="pregnancy">Brejost:</label>
<?php $querry="SELECT * FROM pregnancies WHERE idpregnancies ='".$animal['fk_idpregnancies']."' ";
$stmt = $pdo->prepare($querry);
$stmt->execute();
$pregnancies = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>
<?php foreach($pregnancies as $pregnancy): ?>
<input list="pregnancies" type="text" name="pregnancy" id="pregnancy" value="<?=$pregnancy['pregnancy']?>">
<?php endforeach; ?>
<?php $query = "SELECT * FROM pregnancies";
$stmt = $pdo->prepare($query);
$stmt->execute();
$pregnancies = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>
<datalist id="pregnancies">
<?php foreach ($pregnancies as $pregnancy): ?>
<option value="<?=$pregnancy['pregnancy']?>">
<?php endforeach; ?>
</datalist>
</div>
<div class="form-group">
<label for="health">Zdravje:</label>
<?php $querry="SELECT * FROM health WHERE idhealth = '".$animal['fk_idhealth']."'";
$stmt = $pdo->prepare($querry);
$stmt->execute();
$health = $stmt->fetchAll(PDO::FETCH_ASSOC);?>
<?php foreach($health as $status): ?>
<input list="health" type="text" name="health" id="heal" value="<?=$status['status']?>">
<?php endforeach; ?>
<?php $query = "SELECT * FROM health";
$stmt = $pdo->prepare($query);
$stmt->execute();
$health = $stmt->fetchAll(PDO::FETCH_ASSOC); ?>
<datalist id="health">
<?php foreach ($health as $status): ?>
<option value="<?=$status['status']?>">
<?php endforeach; ?>
</datalist>
</div>
<?php if(is_admin() || $_SESSION['user_id'] == $animal['fk_idusers']): ?>
<div class="col-md-6">
<button type="submit" name="update" class="btn btn-primary" id="update" value="Shrani">Shrani</button>
</div>
<?php endif; ?>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$("#update").click(function(e){
e.preventDefault();
var username = $( "#owner" ).val();
var pregnancy = $( "#pregnancy" ).val();
var health = $( "#heal" ).val();
console.log(username);
console.log(pregnancy);
console.log(health);
$.ajax({
type: 'POST',
data: 'username=' + username + '&pregnancy=' + pregnancy + '&health=' + health,
url:'./inc/animal.update.inc.php',
success:function(data){
alert(data);
}
});
})
</script>
<?=template_footer()?>