-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_share.php
192 lines (165 loc) · 5.58 KB
/
edit_share.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
<?php
session_start();
$page = "Edit Share";
/*
protect route
check if user is not authenticated
to redirect the user to login page
*/
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
}
// require database connection file
require_once 'config/db.php';
$share_id = $_GET['share_id'];
$user_id = $_SESSION['user_id'];
$error = false;
$food_name_err = $photo_err = $description_err = "";
$photo = $food_name = $description = "";
//prepare an sql select query
$query = "SELECT * FROM foods WHERE id=:id AND user_id=:user_id";
if ($data = $pdo->prepare($query)) {
$data->bindParam(":id", $share_id, PDO::PARAM_INT);
$data->bindParam(":user_id", $user_id, PDO::PARAM_INT);
// attempt to execute
if($data->execute()){
// check if share exist
if ($data->rowCount() === 1) {
$result = $data->fetch();
// reset variables
$food_name = $result['food_name'];
$description = $result["description"];
}else{
$message = "Share does not exist";
}
}else{
$message = "Error fetching data";
}
}
if($_SERVER['REQUEST_METHOD'] === "POST") {
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
// put POSTS vars into regular variable
$photo = $_FILES["photo"]["name"];
$photo_temp = $_FILES["photo"]["tmp_name"];
$food_name = trim($_POST["food_name"]);
$description = trim($_POST["description"]);
$target_dir = "assets/images/";
if (empty($photo)) {
$photo = $result['photo'];
}else{
// check for valid image extension
switch($_FILES['photo']['type']){
case 'image/jpeg':
case 'image/jpg':
$ext = 'jpg';
break;
case 'image/gif':
$ext = 'gif';
break;
case 'image/png':
$ext = 'png';
break;
case 'image/tiff':
$ext = 'tif';
break;
case 'image/webp':
$ext = 'webp';
break;
default:
$ext = '';
break;
}
// validate extension
if(!$ext){
$error = true;
$photo_err = "Sorry, only JPG, JPEG, PNG, webp & GIF files are allowed.";
}
// rename image before inserting to database
$photo = "oaccfl_".date('h_i_s').time().".".$ext;
}
if(empty($food_name)){
$error = true;
$food_name_err = "Please enter food name";
}
if(empty($description)){
$error = true;
$description_err = "Please enter food description";
}
// if there is no error
if(!$error){
$date_created = date("dS M, Y h:i:a");
// preapare an sql insert query
$sql = "UPDATE foods SET food_name=:food_name, description=:description, photo=:photo WHERE id=:share_id AND user_id=:user_id";
// bind params
if($stmt = $pdo->prepare($sql)){
$stmt->bindParam(":food_name", $food_name, PDO::PARAM_STR);
$stmt->bindParam(":description", $description, PDO::PARAM_STR);
$stmt->bindParam(":photo", $photo, PDO::PARAM_STR);
$stmt->bindParam(":share_id", $share_id, PDO::PARAM_STR);
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
// only upload image if user select a new image
if(!empty($_FILES['photo']['name'])){
move_uploaded_file($photo_temp, $target_dir.$photo);
}
// prepare to execute
if($stmt->execute()){
$_SESSION['success-msg'] = "Share updated sucessfully";
// redirect to my shares
header("Location: myshares.php");
}else{
die("Error: executing code");
}
}
// close connection
unset($stmt);
}
}
require_once 'includes/head.php';
require_once 'includes/nav.php';
?>
<!-- start main content -->
<div class="container-fluid">
<div class="row">
<div class="col-md-3 mt-4">
<?php require_once 'includes/sidebar.php'; ?>
</div>
<div class="col-md-9 mt-4">
<h3>Edit food information</h3>
<p>Fill in the form to edit food information</p>
<hr>
<div class="row">
<div class="col-md-6">
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']).'?share_id='.$share_id; ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="photo">Photo:</label>
<input type="file" name="photo" class="form-control <?php echo (!empty($photo_err)) ? 'is-invalid' : ''; ?>" id="photo" >
<span class="invalid-feedback"><?php echo $photo_err ?></span>
</div>
<div class="form-group">
<label for="food_name">Food Name:</label>
<input type="text" name="food_name" class="form-control <?php echo (!empty($food_name_err)) ? 'is-invalid' : ''; ?>" id="food_name" placeholder="Food Name" value="<?php echo $food_name; ?>">
<span class="invalid-feedback"><?php echo $food_name_err ?></span>
</div>
<div class="form-group">
<label for="description">Description:</label>
<textarea name="description" id="description" data-toggle="tooltip" title="maximum of 150 characters" maxlength="150" rows="4" class="form-control <?php echo (!empty($description_err)) ? 'is-invalid' : ''; ?>" placeholder="Food description"><?php echo $description; ?></textarea>
<span class="invalid-feedback"><?php echo $description_err; ?></span>
</div>
<button type="submit" class="btn btn-success">Update</button>
<a href="myshares.php" style="color: #fff" class="btn btn-warning">Cancel</a>
</form>
<br><br>
</div>
<div class="col-md-6">
<h5>Food Image:</h5>
<img src="assets/images/<?php echo $result['photo']; ?>" style="height: 250px" alt="<?php echo $food_name; ?>" class="img-fluid">
</div>
</div>
</div>
</div>
</div>
<!-- end main content -->
<?php
require_once 'includes/footer.php';
require_once 'includes/foot.php';
?>