-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path10.wines.php
59 lines (46 loc) · 1.58 KB
/
10.wines.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
<?php
if (basename($_SERVER[SCRIPT_NAME]) != "10.results.php") {
header("Location: 10.html_form.php");
exit;
}
require 'db_secure.php';
// get all wines
function getWines($regionName) {
// Connect to the server
if (!($connection = @ mysql_connect(DB_HOST, DB_USER, DB_PW))) {
showerror();
}
if (!mysql_select_db(DB_NAME, $connection)) {
showerror();
}
// manually clean data
$regionName = substr($regionName, 0, 30);
$regionName = mysql_real_escape_string($regionName, $connection);
// Start a query ...
$query = "SELECT wine_id, wine_name, description, year, winery_name
FROM winery, region, wine
WHERE winery.region_id = region.region_id
AND wine.winery_id = winery.winery_id";
// ... then, if the user has specified a region, add the regionName
// as an AND clause ...
if (isset($regionName) && $regionName != "All") {
$query .= " AND region_name = '{$regionName}'";
}
// ... and then complete the query.
$query .= " ORDER BY wine_name";
// Run the query on the server
if (!($result = @ mysql_query ($query, $connection))) {
showerror();
}
// Find out how many rows are available
$rowsFound = @ mysql_num_rows($result);
$wines = array();
// If the query has results ...
if ($rowsFound > 0) {
// Fetch each of the query rows
while ($row = @ mysql_fetch_assoc($result)) {
$wines[$row['wine_id']] = $row;
} // end while loop body
} // end if $rowsFound body
return $wines;
} // end of function