-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
154 lines (127 loc) · 4.18 KB
/
search.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
<?php
function buildClause($strarr) {
$last = end($strarr);
$clause = "";
foreach ($strarr as $cur) {
if ($cur == $last) {
$clause = $clause . " " . $cur;
} else {
$clause = $clause . " " . $cur . " AND";
}
}
return $clause;
};
?>
<html>
<head>
<?php $title = "IMDB: I(ncomplete and Dated) Movie DB" ?>
<title><?php print "$title"; ?></title>
<link rel="stylesheet" type="text/css" href="./style.css">
</head>
<body bgcolor=white>
<a href="search.php"><h1><?php print "$title"; ?></h1></a>
<nav>
<a href="./search.php">Search</a>
<a href="./Add_A_or_D.php">Add Actor or Director</a>
<a href="./Add_M.php">Add Movie</a>
<a href="./Add_A_M_relation.php">Add Actor/Movie Relation</a>
<a href="./Add_D_M_relation.php">Add Director/Movie Relation</a>
</nav>
<div class="content">
<hr>
<h2>Search Actors and Movies</h2>
<form action="./search.php" method="GET">
<input type="text" name="query" size="40" <br>
<input type="submit" value="Submit">
</form>
<?php
$query = $_GET["query"];
if (!empty($_GET)) {
$db = new mysqli('localhost', 'cs143', '', 'CS143');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
// Deal with multiple space characters. Also applies set semantics to query tokens.
$multipleSpaces = preg_match('/ /', $query) || ($query == " ");
$query_tokens = explode(" ", $query);
$query_tokens = array_unique($query_tokens);
if ($multipleSpaces) {
array_push($query_tokens, " ");
}
// Escape the tokens and then build the appropriate constraints from the tokens
$actorParameters = array_map(function($str) use ($db) {
$escaped_str = $db->real_escape_string($str);
return "((first LIKE '%$escaped_str%') OR (last LIKE '%$escaped_str%'))";
}, $query_tokens);
$movieParameters = array_map(function($str) use ($db) {
$escaped_str = $db->real_escape_string($str);
return "(title LIKE '%$escaped_str%')";
}, $query_tokens);
$actorQuery = "SELECT * FROM Actor WHERE" . buildClause($actorParameters);
$movieQuery = "SELECT * FROM Movie WHERE" . buildClause($movieParameters);
print "<br><hr>";
if (!($rs = $db->query($actorQuery))) {
$errmsg = $db->error;
print "Query failed: $errmsg <br/>";
} else {
print "<h2> Actor Results </h2> <br/>";
print '<table border="1" cellpadding="2" cellspacing="1">';
$fields = $rs->fetch_fields();
print '<tr align="center">';
foreach ($fields as $column) {
print '<td><b>';
print $column->name;
print '</b></td>';
}
print '</tr>';
while($row = $rs->fetch_assoc()) {
$id = $row['id'];
print '<tr align=center>';
foreach($row as $_ => $value) {
print '<td>';
print "<a href='Show_A.php?identifier=$id'>";
print $value;
print '</a>';
print '</td>';
}
print '</tr>';
}
print '</table>';
print "<br><hr>";
}
$rs->free();
if (!($rs = $db->query($movieQuery))) {
$errmsg = $db->error;
print "Query failed: $errmsg <br />";
} else {
print "<h2> Movie Results </h2> <br/>";
print '<table border="1" cellpadding="2" cellspacing="1">';
$fields = $rs->fetch_fields();
print '<tr align="center">';
foreach ($fields as $column) {
print '<td><b>';
print $column->name;
print '</b></td>';
}
print '</tr>';
while($row = $rs->fetch_assoc()) {
$mid = $row["id"];
print '<tr align=center>';
foreach($row as $_ => $value) {
print '<td>';
print "<a href='Show_M.php?identifier=$mid'>";
print $value;
print '</a>';
print '</td>';
}
print '</tr>';
}
print '</table>';
}
$rs->free();
$db->close();
}
?>
</div>
</body>
</html>