forked from JP-kazne/workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
79 lines (66 loc) · 2.75 KB
/
Copy pathindex.php
File metadata and controls
79 lines (66 loc) · 2.75 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SQL_Challenge_2</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<header>
<h1>年別アニメ視聴リスト</h1>
</header>
<div class="content">
<section class="year">
<h1>
<ul class="clearfix">
<li><a href="index.php?year=2011">2011</a></li>
<li><a href="index.php?year=2012">2012</a></li>
<li><a href="index.php?year=2013">2013</a></li>
<li><a href="index.php?year=2014">2014</a></li>
<li><a href="index.php?year=2015">2015</a></li>
<li><a href="index.php?year=2016">2016</a></li>
</ul>
</h1>
</section>
<section class="list">
<table border="1" class="clearfix">
<tr>
<th>title</th>
<th>year</th>
</tr>
<?php
//mysqlへ接続する。
$mysqli = new mysqli('mysql', 'wani', 'waniwani', 'test_db');
if ($mysqli->connect_error) {
echo $mysqli->connect_error;
exit();
} else {
$mysqli->set_charset("utf8mb4");
}
//urlの"year="の後に入力した文字列を$yearに入れる。
$year = $_GET["year"];
//preg_replaceで危険な記号を処理する。
$pattern = '/([^a-zA-Z0-9])/';
$replace = '\\\$0';
$year = preg_replace($pattern, $replace, $year);
//クエリを作成する。
$query = "SELECT * FROM anime WHERE years = $year";
//debug用にhtmlのコメントにクエリを表示させる。
echo "<!-- debug : ", htmlspecialchars($query), " -->\n";
//結果を表示させる。
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo '<tr><th>' . $row['name'] . '</th><th>' . $row['years'] . '</th></tr>';
}
$result->close();
}
?>
</table>
</section>
</div>
</div>
</body>
</html>