Skip to content

Commit d688f99

Browse files
committed
Merge branch 'img_upload'
2 parents 8f5028d + c2fa979 commit d688f99

34 files changed

+135
-35
lines changed

css/style.css

+5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ main article {
181181
color: black;
182182
}
183183

184+
#restaurants img {
185+
width: 40em;
186+
height: 20em;
187+
}
188+
184189
.category {
185190
display: flex;
186191
flex-wrap: wrap;

database/data-fetching/dishes.php

+9
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ function getDishsByOrder($db, $orderid){
3333
$dishes = $stmt->fetchAll();
3434
return $dishes;
3535
}
36+
37+
function getDishes($db){
38+
$stmt = $db ->prepare(
39+
'SELECT dishID FROM Dish'
40+
);
41+
$stmt->execute();
42+
$dishes = $stmt->fetchAll();
43+
return $dishes;
44+
}
3645
?>

database/data-insertion/insert-new-restaurant.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
2-
function addRestaurantToDatabase($db, $name, $description, $category, $email, $phone, $address, $ownerID) {;
2+
function addRestaurantToDatabase($db, $name, $description, $category, $email, $phone, $address, $ownerID,$picture) {;
33
$stmt = $db->prepare(
4-
'INSERT INTO Restaurant (name, description, category, email, phoneNumber, address, ownerID) Values(
4+
'INSERT INTO Restaurant (name, description, category, email, phoneNumber, address, ownerID, picture) Values(
55
:name,
66
:description,
77
:category,
88
:email,
99
:phone,
1010
:address,
11-
:ownerID
11+
:ownerID,
12+
:picture
1213
)'
1314
);
1415
$stmt->bindParam(':name', $name);
@@ -18,6 +19,7 @@ function addRestaurantToDatabase($db, $name, $description, $category, $email, $p
1819
$stmt->bindParam(':phone', $phone);
1920
$stmt->bindParam(':address', $address);
2021
$stmt->bindParam(':ownerID', $ownerID);
22+
$stmt->bindParam(':picture', $picture);
2123

2224
$stmt->execute();
2325
}

database/data-insertion/update-restaurant-info.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
2-
function updateRestaurantInfo($db, $restaurantID, $name, $description, $category, $email, $phone, $address) {
2+
function updateRestaurantInfo($db, $restaurantID, $name, $description, $category, $email, $phone, $address,$picture) {
33
$stmt = $db->prepare(
44
'UPDATE Restaurant
55
SET name = :name, description = :description,
66
category = :category, email = :email,
7-
phoneNumber = :phone, address = :address
7+
phoneNumber = :phone, address = :address, picture = :picture
88
WHERE restaurantID = :id'
99
);
1010
$stmt->bindParam(':id', $restaurantID);
@@ -14,6 +14,7 @@ function updateRestaurantInfo($db, $restaurantID, $name, $description, $category
1414
$stmt->bindParam(':email', $email);
1515
$stmt->bindParam(':phone', $phone);
1616
$stmt->bindParam(':address', $address);
17+
$stmt->bindParam(':picture',$picture);
1718

1819
$stmt->execute();
1920
}

database/restaurants.sql

+23-15
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ CREATE TABLE Restaurant(
2929
email VARCHAR(32),
3030
phoneNumber CHAR(9) NOT NULL,
3131
category VARCHAR(16),
32-
ownerID INTEGER REFERENCES USER(userID)
32+
ownerID INTEGER REFERENCES USER(userID),
33+
picture TEXT NOT NULL
3334
);
3435

3536
CREATE TABLE Dish(
3637
dishID INTEGER PRIMARY KEY,
3738
name VARCHAR(32) NOT NULL,
3839
description TEXT NOT NULL,
3940
price FLOAT NOT NULL,
40-
picture TEXT NOT NULL,
41+
picture TEXT NOT NULL,
4142
category VARCHAR(32) NOT NULL,
4243
restaurantID INTEGER REFERENCES Restaurant(restaurantID)
4344
);
@@ -117,7 +118,8 @@ INSERT INTO Restaurant VALUES(
117118
118119
'225081401',
119120
'Family Style',
120-
2
121+
2,
122+
'restaurant1.png'
121123
);
122124

123125
INSERT INTO Restaurant VALUES(
@@ -128,7 +130,8 @@ INSERT INTO Restaurant VALUES(
128130
129131
'225081402',
130132
'Desert',
131-
2
133+
2,
134+
'restaurant2.png'
132135
);
133136

134137
INSERT INTO Restaurant VALUES(
@@ -139,7 +142,8 @@ INSERT INTO Restaurant VALUES(
139142
140143
'225081403',
141144
'Family Style',
142-
3
145+
3,
146+
'restaurant3.png'
143147
);
144148

145149
INSERT INTO Restaurant VALUES(
@@ -151,7 +155,8 @@ INSERT INTO Restaurant VALUES(
151155
152156
'225081404',
153157
'Seafood',
154-
2
158+
2,
159+
'restaurant4.png'
155160
);
156161

157162
INSERT INTO Restaurant VALUES(
@@ -162,7 +167,8 @@ INSERT INTO Restaurant VALUES(
162167
163168
'225081405',
164169
'Seafood',
165-
2
170+
2,
171+
'restaurant5.png'
166172
);
167173

168174
INSERT INTO Restaurant VALUES(
@@ -174,7 +180,8 @@ INSERT INTO Restaurant VALUES(
174180
175181
'225081406',
176182
'Vegetarian',
177-
3
183+
3,
184+
'restaurant6.png'
178185
);
179186

180187
INSERT INTO Restaurant VALUES(
@@ -185,7 +192,8 @@ INSERT INTO Restaurant VALUES(
185192
186193
'225081402',
187194
'Desert',
188-
2
195+
2,
196+
'restaurant7.png'
189197
);
190198

191199

@@ -196,7 +204,7 @@ INSERT INTO Dish VALUES(
196204
'Neapolitan Pizza',
197205
'This is just a Pizza',
198206
22.50,
199-
'picture1.png',
207+
'dish1.png',
200208
'Pizza',
201209
1
202210
);
@@ -206,7 +214,7 @@ INSERT INTO Dish VALUES(
206214
'Pasta',
207215
'Pasta',
208216
5.60,
209-
'picture2.png',
217+
'dish2.png',
210218
'Pasta',
211219
1
212220
);
@@ -216,7 +224,7 @@ INSERT INTO Dish VALUES(
216224
'Burger',
217225
'Burger',
218226
22.50,
219-
'picture3.png',
227+
'dish3.png',
220228
'Burger',
221229
2
222230
);
@@ -226,7 +234,7 @@ INSERT INTO Dish VALUES(
226234
'Cheese Pizza',
227235
'This is just a Pizza',
228236
22.50,
229-
'picture4.png',
237+
'dish4.png',
230238
'Pizza',
231239
1
232240
);
@@ -236,7 +244,7 @@ INSERT INTO Dish VALUES(
236244
'Veggie Pizza',
237245
'This is just a Pizza',
238246
22.50,
239-
'picture5.png',
247+
'dish5.png',
240248
'Pizza',
241249
1
242250
);
@@ -246,7 +254,7 @@ INSERT INTO Dish VALUES(
246254
'Pepperoni Pizza',
247255
'This is just a Pizza',
248256
22.50,
249-
'picture6.png',
257+
'dish6.png',
250258
'Pizza',
251259
1
252260
);

img/dish1.png

1.1 MB
Loading

img/dish2.png

580 KB
Loading

img/dish3.png

1.1 MB
Loading

img/dish4.png

1.1 MB
Loading

img/dish5.png

1.1 MB
Loading

img/dish6.png

1.1 MB
Loading

img/dish7.png

1.1 MB
Loading

img/restaurant1.png

580 KB
Loading

img/restaurant2.png

580 KB
Loading

img/restaurant3.png

580 KB
Loading

img/restaurant4.png

580 KB
Loading

img/restaurant5.png

580 KB
Loading

img/restaurant6.png

580 KB
Loading

img/restaurant7.png

580 KB
Loading

img/restaurant8.png

555 KB
Loading

php/actions/add-dish.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
require_once('../../database/data-fetching/user.php');
44
require_once('../../database/data-fetching/restaurants.php');
55
require_once('../../database/data-insertion/insert-new-dish.php');
6-
6+
require_once('img-insertion.php');
7+
require_once('../../database/data-fetching/dishes.php');
8+
require_once('../../database/data-insertion/update-dish-info.php');
9+
session_start();
10+
711
$db = getDatabaseConnection('../../database/restaurants.db');
812
$user_info = getUserbyID($db, $_SESSION['userID']);
913
$restaurant_info = getRestaurantInfo($db, $_GET['restaurantID']);
@@ -19,9 +23,14 @@
1923
$description = $_POST['description'];
2024
$price = $_POST['price'];
2125
$category = $_POST['category'];
22-
$picture = $_POST['picture'];
26+
$picture = "picture1";
2327
$restaurantID = $_GET['restaurantID'];
2428

2529
addDishToDatabase($db, $name, $description, $price, $picture, $category, $restaurantID);
30+
$dishID =count(getDishes($db));
31+
$dish_info = getDishInfo($db, $dishID);
32+
$picture = insertImage($dish_info);
33+
updateDishInfo($db, $dishID, $name, $description, $price, $category, $picture);
34+
2635
header('Location: ../../restaurant-page.php?id=' . $restaurantID);
2736
?>

php/actions/add-restaurant.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
require_once('../../database/db-connection.php');
33
require_once('../../database/data-fetching/user.php');
44
require_once('../../database/data-insertion/insert-new-restaurant.php');
5+
require_once('../../database/data-fetching/restaurants.php');
6+
require_once('../../database/data-insertion/update-restaurant-info.php');
7+
require_once('img-insertion-restaurants.php');
58

69
session_start();
710
$db = getDatabaseConnection('../../database/restaurants.db');
@@ -18,7 +21,13 @@
1821
$email = $_POST['email'];
1922
$phone = $_POST['phone'];
2023
$address = $_POST['address'];
24+
$picture = "picture1";
25+
26+
addRestaurantToDatabase($db, $name, $description, $category, $email, $phone, $address, $_SESSION['userID'],$picture);
27+
$restaurantID = count(getAllRestaurants($db));
28+
$restaurant_info = getRestaurantInfo($db, $restaurantID);
29+
$picture = insertImageRestaurant($restaurant_info);
30+
updateRestaurantInfo($db, $restaurantID, $name, $description, $category, $email, $phone, $address, $picture);
2131

22-
addRestaurantToDatabase($db, $name, $description, $category, $email, $phone, $address, $_SESSION['userID']);
2332
header('Location: ../../user-info-page.php');
2433
?>

php/actions/edit-dish-info.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_once('../../database/data-fetching/dishes.php');
55
require_once('../../database/data-fetching/restaurants.php');
66
require_once('../../database/data-insertion/update-dish-info.php');
7+
require_once('img-insertion.php');
78

89
session_start();
910
$db = getDatabaseConnection('../../database/restaurants.db');
@@ -24,9 +25,7 @@
2425
$category = $_POST['category'];
2526
$picture = $_POST['picture'];
2627

27-
if ($picture === '') {
28-
$picture = $dish_info['picture'];
29-
}
28+
$picture = insertImage($dish_info);
3029

3130
updateDishInfo($db, $_GET['dishID'], $name, $description, $price, $category, $picture);
3231
header('Location: ../../restaurant-page.php?id=' . $_GET['restaurantID']);

php/actions/edit-restaurant-info.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require_once('../../database/db-connection.php');
33
require_once('../../database/data-fetching/restaurants.php');
44
require_once('../../database/data-insertion/update-restaurant-info.php');
5+
require_once('img-insertion-restaurants.php');
56

67
session_start();
78
$restaurantID = $_GET['id'];
@@ -17,7 +18,8 @@
1718
$email = $_POST['email'];
1819
$phone = $_POST['phone'];
1920
$address = $_POST['address'];
21+
$picture = insertImageRestaurant($current_restaurant_info);
2022

21-
updateRestaurantInfo($db, $restaurantID, $name, $description, $category, $email, $phone, $address);
23+
updateRestaurantInfo($db, $restaurantID, $name, $description, $category, $email, $phone, $address,$picture);
2224
header('Location: ../../restaurant-page.php?id=' . $restaurantID);
2325
?>
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
function insertImageRestaurant($restaurant_info){
3+
$target_dir = "../../img/";
4+
$target_file = $target_dir . basename($_FILES["picture"]["name"]);
5+
$uploadOk = 1;
6+
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
7+
8+
$check = getimagesize($_FILES["picture"]["tmp_name"]);
9+
if($check !== false) {
10+
$uploadOk = 1;
11+
} else {
12+
$uploadOk = 0;
13+
}
14+
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
15+
$uploadOk = 0;
16+
17+
}
18+
if ($uploadOk == 0) {
19+
$picture = $restaurant_info['picture'];
20+
} else {
21+
$picture = "restaurant".$restaurant_info['restaurantID'].".".$imageFileType;
22+
move_uploaded_file($_FILES["picture"]["tmp_name"], $target_dir."restaurant".$restaurant_info['restaurantID'].".".$imageFileType); {
23+
}
24+
}
25+
return $picture;
26+
}
27+
?>

php/actions/img-insertion.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
function insertImage($dish_info){
3+
$target_dir = "../../img/";
4+
$target_file = $target_dir . basename($_FILES["picture"]["name"]);
5+
$uploadOk = 1;
6+
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
7+
8+
$check = getimagesize($_FILES["picture"]["tmp_name"]);
9+
if($check !== false) {
10+
$uploadOk = 1;
11+
} else {
12+
$uploadOk = 0;
13+
}
14+
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
15+
$uploadOk = 0;
16+
17+
}
18+
if ($uploadOk == 0) {
19+
$picture = $dish_info['picture'];
20+
} else {
21+
$picture = "dish".$dish_info['dishID'].".".$imageFileType;
22+
move_uploaded_file($_FILES["picture"]["tmp_name"], $target_dir."dish".$dish_info['dishID'].".".$imageFileType); {
23+
}
24+
}
25+
return $picture;
26+
}
27+
?>

php/actions/restaurants.db

Whitespace-only changes.

php/output-functions/draw-dish-edit-form.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ function outputDishEditForm() {
1818
} ?>
1919
<main>
2020
<section id="dishInfo">
21-
<form action="../../php/actions/edit-dish-info.php?restaurantID=<?=$_GET['restaurantID']?>&dishID=<?=$_GET['dishID']?>" method="post">
21+
<form action="../../php/actions/edit-dish-info.php?restaurantID=<?=$_GET['restaurantID']?>&dishID=<?=$_GET['dishID']?>" method="post" enctype="multipart/form-data">
2222
Name: <input type="text" name="name" value="<?=$dish_info['name']?>">
2323
Description: <input type="text" name="description" value="<?=$dish_info['description']?>">
2424
Price: <input type="number" name="price" step="0.01" value="<?=$dish_info['price']?>">
2525
Category: <input type="text" name="category" value="<?=$dish_info['category']?>">
26-
Image: <input type="file" , name="picture">
26+
Image: <input type="file" , name="picture" id="picture">
2727
<input class="submit" type="submit" value="Submit">
2828
</form>
2929
</section>

php/output-functions/draw-dish-register-form.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ function outputDishRegisterForm() {
1616
} ?>
1717
<main>
1818
<section id="dishInfo">
19-
<form action="../../php/actions/add-dish.php?restaurantID=<?=$_GET['restaurantID']?>" method="post">
19+
<form action="../../php/actions/add-dish.php?restaurantID=<?=$_GET['restaurantID']?>" method="post" enctype="multipart/form-data">
2020
Name: <input type="text" name="name">
2121
Description: <input type="text" name="description">
2222
Price: <input type="number" name="price" step="0.01">
2323
Category: <input type="text" name="category">
24-
Image: <input type="file" , name="picture">
24+
Image: <input type="file" , name="picture" id="picture">
2525
<input class="submit" type="submit" value="Submit">
2626
</form>
2727
</section>

0 commit comments

Comments
 (0)