質問編集履歴
2
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,3 +7,373 @@
|
|
7
7
|
このエラーを調べてもMySQLでの対処法が多く載っていましたがMySQLは一切使ってないのでMAMPでの対策が知りたいです。
|
8
8
|
|
9
9
|
どなたかご教授願います。
|
10
|
+
|
11
|
+
以下にindex.phpのコードを載せたいと思います。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
<------index.php------->
|
16
|
+
|
17
|
+
<?php
|
18
|
+
|
19
|
+
session_start();
|
20
|
+
|
21
|
+
require('dbconnect.php');
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
if (isset($_SESSION['id']) && $_SESSION['time'] + 3600 > time()) {
|
26
|
+
|
27
|
+
// ログインしている
|
28
|
+
|
29
|
+
$_SESSION['time'] = time();
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
$sql = sprintf('SELECT * FROM members WHERE id=%d',
|
34
|
+
|
35
|
+
mysqli_real_escape_string($db, $_SESSION['id'])
|
36
|
+
|
37
|
+
);
|
38
|
+
|
39
|
+
$record = mysqli_query($db, $sql) or die(mysqli_error($db));
|
40
|
+
|
41
|
+
$member = mysqli_fetch_assoc($record);
|
42
|
+
|
43
|
+
} else {
|
44
|
+
|
45
|
+
// ログインしていない
|
46
|
+
|
47
|
+
header('Location: login.php');
|
48
|
+
|
49
|
+
exit();
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
// 投稿を記録する
|
56
|
+
|
57
|
+
if (!empty($_POST)) {
|
58
|
+
|
59
|
+
if ($_POST['message'] != '') {
|
60
|
+
|
61
|
+
$sql = sprintf('INSERT INTO posts SET member_id=%d, message="%s", reply_post_id=%d, created=NOW()',
|
62
|
+
|
63
|
+
mysqli_real_escape_string($db, $member['id']),
|
64
|
+
|
65
|
+
mysqli_real_escape_string($db, $_POST['message']),
|
66
|
+
|
67
|
+
mysqli_real_escape_string($db, $_POST['reply_post_id'])
|
68
|
+
|
69
|
+
);
|
70
|
+
|
71
|
+
mysqli_query($db, $sql) or die(mysqli_error($db));
|
72
|
+
|
73
|
+
header('Location: index.php');
|
74
|
+
|
75
|
+
exit();
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
// 投稿を取得する
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
$page = $_REQUEST['page'];
|
88
|
+
|
89
|
+
if ($page == '') {
|
90
|
+
|
91
|
+
$page = 1;
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
$page = max($page, 1);
|
96
|
+
|
97
|
+
// 最終ページを取得する
|
98
|
+
|
99
|
+
$sql = 'SELECT COUNT(*) AS cnt FROM posts';
|
100
|
+
|
101
|
+
$recordSet = mysqli_query($db, $sql);
|
102
|
+
|
103
|
+
$table = mysqli_fetch_assoc($recordSet);
|
104
|
+
|
105
|
+
$maxPage = ceil($table['cnt'] / 5);
|
106
|
+
|
107
|
+
$page = min($page, $maxPage);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
$start = ($page - 1) * 5;
|
112
|
+
|
113
|
+
$start = max(0, $start);
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
$sql = sprintf('SELECT m.name, m.picture, p.* FROM members m, posts p WHERE m.id=p.member_id ORDER BY p.created DESC LIMIT %d, 5',
|
120
|
+
|
121
|
+
$start
|
122
|
+
|
123
|
+
);
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
$posts = mysqli_query($db, $sql) or die(mysqli_error($db));
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
// 返信の場合
|
132
|
+
|
133
|
+
if (isset($_REQUEST['res'])) {
|
134
|
+
|
135
|
+
$sql = sprintf('SELECT m.name, m.picture, p.* FROM members m, posts p WHERE m.id=p.member_id AND p.id=%d ORDER BY p.created DESC',
|
136
|
+
|
137
|
+
mysqli_real_escape_string($db, $_REQUEST['res'])
|
138
|
+
|
139
|
+
);
|
140
|
+
|
141
|
+
$record = mysqli_query($db, $sql) or die(mysqli_error($db));
|
142
|
+
|
143
|
+
$table = mysqli_fetch_assoc($record);
|
144
|
+
|
145
|
+
$message = '@' . $table['name'] . ' ' . $table['message'];
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
// htmlspecialcharsのショートカット
|
152
|
+
|
153
|
+
function h($value) {
|
154
|
+
|
155
|
+
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
// 本文内のURLにリンクを設定します
|
162
|
+
|
163
|
+
function makeLink($value) {
|
164
|
+
|
165
|
+
return mb_ereg_replace("(https?)(://[[:alnum:]\+\$\;\?\.%,!#~*/:@&=_-]+)", '<a href="\1\2">\1\2</a>' , $value);
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
?>
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
174
|
+
|
175
|
+
<html xmlns="http://www.w3.org/1999/xhtml">
|
176
|
+
|
177
|
+
<head>
|
178
|
+
|
179
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
180
|
+
|
181
|
+
<link rel="stylesheet" type="text/css" href="style.css" />
|
182
|
+
|
183
|
+
<title>ひとこと掲示板</title>
|
184
|
+
|
185
|
+
</head>
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
<body>
|
190
|
+
|
191
|
+
<div id="wrap">
|
192
|
+
|
193
|
+
<div id="head">
|
194
|
+
|
195
|
+
<h1>ひとこと掲示板</h1>
|
196
|
+
|
197
|
+
</div>
|
198
|
+
|
199
|
+
<div id="content">
|
200
|
+
|
201
|
+
<div style="text-align: right"><a href="logout.php">ログアウト</a></div>
|
202
|
+
|
203
|
+
<form action="" method="post">
|
204
|
+
|
205
|
+
<dl>
|
206
|
+
|
207
|
+
<dt><?php echo htmlspecialchars($member['name']); ?>さん、メッセージをどうぞ</dt>
|
208
|
+
|
209
|
+
<dd>
|
210
|
+
|
211
|
+
<textarea name="message" cols="50" rows="5"><?php echo h($message, ENT_QUOTES, 'UTF-8'); ?></textarea>
|
212
|
+
|
213
|
+
<input type="hidden" name="reply_post_id" value="<?php echo h($_REQUEST['res'], ENT_QUOTES, 'UTF-8'); ?>" />
|
214
|
+
|
215
|
+
</dd>
|
216
|
+
|
217
|
+
</dl>
|
218
|
+
|
219
|
+
<div>
|
220
|
+
|
221
|
+
<p>
|
222
|
+
|
223
|
+
<input type="submit" value="投稿する" />
|
224
|
+
|
225
|
+
</p>
|
226
|
+
|
227
|
+
</div>
|
228
|
+
|
229
|
+
</form>
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
<?php
|
234
|
+
|
235
|
+
while($post = mysqli_fetch_assoc($posts)):
|
236
|
+
|
237
|
+
?>
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
<div class="msg">
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
<img src="member_picture/<?php echo h($post['picture']); ?>" width="48" height="48" alt="<?php echo h($post['name']); ?>" />
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
<p><?php echo makeLink(h($post['message'])); ?><span class="name">(<?php echo h($post['name']); ?>)</span>[<a href="index.php?res=<?php echo h($post['id']); ?>">Re</a>]</p>
|
250
|
+
|
251
|
+
<p class="day"><a href="view.php?id=<?php echo h($post['id']); ?>"><?php echo h($post['created']); ?></a>
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
<?php
|
256
|
+
|
257
|
+
if ($post['reply_post_id'] > 0):
|
258
|
+
|
259
|
+
?>
|
260
|
+
|
261
|
+
<a href="view.php?id=<?php echo h($post['reply_post_id']);
|
262
|
+
|
263
|
+
?>">返信元のメッセージ</a>
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
<?php
|
268
|
+
|
269
|
+
endif;
|
270
|
+
|
271
|
+
?>
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
<?php
|
276
|
+
|
277
|
+
if ($_SESSION['id'] == $post['member_id']):
|
278
|
+
|
279
|
+
?>
|
280
|
+
|
281
|
+
[<a href="delete.php?id=<?php echo h($post['id']); ?>" style="color: #F33;">削除</a>]
|
282
|
+
|
283
|
+
<?php
|
284
|
+
|
285
|
+
endif;
|
286
|
+
|
287
|
+
?>
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
</p>
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
</div>
|
300
|
+
|
301
|
+
<?php
|
302
|
+
|
303
|
+
endwhile;
|
304
|
+
|
305
|
+
?>
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
<ul class="paging">
|
310
|
+
|
311
|
+
<?php
|
312
|
+
|
313
|
+
if ($page > 1) {
|
314
|
+
|
315
|
+
?>
|
316
|
+
|
317
|
+
<li><a href="index.php?page=<?php print($page - 1); ?>">前のページへ
|
318
|
+
|
319
|
+
</a></li>
|
320
|
+
|
321
|
+
<?php
|
322
|
+
|
323
|
+
} else {
|
324
|
+
|
325
|
+
?>
|
326
|
+
|
327
|
+
<li>前のページへ</li>
|
328
|
+
|
329
|
+
<?php
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
?>
|
334
|
+
|
335
|
+
<?php
|
336
|
+
|
337
|
+
if ($page < $maxPage) {
|
338
|
+
|
339
|
+
?>
|
340
|
+
|
341
|
+
<li><a href="index.php?page=<?php print($page + 1); ?>">次のページへ
|
342
|
+
|
343
|
+
</a></li>
|
344
|
+
|
345
|
+
<?php
|
346
|
+
|
347
|
+
} else {
|
348
|
+
|
349
|
+
?>
|
350
|
+
|
351
|
+
<li>次のページへ</li>
|
352
|
+
|
353
|
+
<?php
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
?>
|
358
|
+
|
359
|
+
</ul>
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
</div>
|
368
|
+
|
369
|
+
<div id="foot">
|
370
|
+
|
371
|
+
<p><img src="images/txt_copyright.png" width="136" height="15" alt="(C) H2O SPACE, Mynavi" /></p>
|
372
|
+
|
373
|
+
</div>
|
374
|
+
|
375
|
+
</div>
|
376
|
+
|
377
|
+
</body>
|
378
|
+
|
379
|
+
</html>
|
1
文法の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
よくわかるPHPの教科書という本でindex.phpを作成し、そのファイルにアクセスしようとした時に
|
1
|
+
よくわかるPHPの教科書という本のPart5のPractice2でindex.phpを作成し、そのファイルにアクセスしようとした時に
|
2
2
|
|
3
3
|
Access denied for user 'root'@'localhost' (using password: NO)
|
4
4
|
|