回答編集履歴
2
mysqli_stmt_bind_param
test
CHANGED
@@ -290,7 +290,7 @@
|
|
290
290
|
|
291
291
|
$stmt = mysqli_prepare($link, $query);
|
292
292
|
|
293
|
-
mysqli_bind_param($stmt, "sss", $name, $comment, $date);
|
293
|
+
mysqli_stmt_bind_param($stmt, "sss", $name, $comment, $date);
|
294
294
|
|
295
295
|
$result = mysqli_stmt_execute($stmt);
|
296
296
|
|
1
見直し
test
CHANGED
@@ -187,3 +187,215 @@
|
|
187
187
|
</html>
|
188
188
|
|
189
189
|
```
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
で、さらに直した:
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
```php
|
198
|
+
|
199
|
+
<?php
|
200
|
+
|
201
|
+
date_default_timezone_set('Asia/Tokyo');
|
202
|
+
|
203
|
+
$name = null;
|
204
|
+
|
205
|
+
$comment = null;
|
206
|
+
|
207
|
+
$date = date("Y-m-d H:i:s");
|
208
|
+
|
209
|
+
$errors = array();
|
210
|
+
|
211
|
+
$host = '';
|
212
|
+
|
213
|
+
$username = '';
|
214
|
+
|
215
|
+
$passwd = '';
|
216
|
+
|
217
|
+
$dbname = '';
|
218
|
+
|
219
|
+
$link = mysqli_connect($host,$username,$passwd,$dbname);
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
if($link !== FALSE){
|
224
|
+
|
225
|
+
mysqli_set_charset($link, 'utf8');
|
226
|
+
|
227
|
+
$query = 'SELECT name,comment,date FROM comment_table';
|
228
|
+
|
229
|
+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
230
|
+
|
231
|
+
if (isset($_POST['name'])) {
|
232
|
+
|
233
|
+
if (mb_strlen($_POST['name']) === 0) {
|
234
|
+
|
235
|
+
$errors['name'] = '名前を入力してください';
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
elseif (mb_strlen($_POST['name']) > 20) {
|
240
|
+
|
241
|
+
$errors['name'] = '名前は20文字以内で入力してください';
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
else {
|
246
|
+
|
247
|
+
$name = $_POST['name'];
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
else {
|
254
|
+
|
255
|
+
$errors['name'] = '名前を入力してください';
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
if (isset($_POST['comment']) !== TRUE) {
|
264
|
+
|
265
|
+
if (mb_strlen($_POST['comment']) === 0) {
|
266
|
+
|
267
|
+
$errors['comment'] = 'ひとことを入力してください';
|
268
|
+
|
269
|
+
} elseif (mb_strlen($_POST['comment']) > 20) {
|
270
|
+
|
271
|
+
$errors['comment'] = 'ひとことは100文字以内で入力してください';
|
272
|
+
|
273
|
+
} else {
|
274
|
+
|
275
|
+
$name = $_POST['comment'];
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
else {
|
282
|
+
|
283
|
+
$errors['comment'] = 'ひとことを入力してください';
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
if (!is_null($name) && !is_null($comment)) {
|
288
|
+
|
289
|
+
$query = 'INSERT INTO `comment_table`(`name`, `comment`, `date`) VALUES(?, ?, ?)';
|
290
|
+
|
291
|
+
$stmt = mysqli_prepare($link, $query);
|
292
|
+
|
293
|
+
mysqli_bind_param($stmt, "sss", $name, $comment, $date);
|
294
|
+
|
295
|
+
$result = mysqli_stmt_execute($stmt);
|
296
|
+
|
297
|
+
if ($result === FALSE) {
|
298
|
+
|
299
|
+
$errors[] = '登録に失敗しました';
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
$data = array();
|
308
|
+
|
309
|
+
$query = 'SELECT `name`, `comment`, `date` FROM `comment_table` ORDER BY `date` DESC';
|
310
|
+
|
311
|
+
$result = mysqli_query($link,$query);
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
while($row = mysqli_fetch_array($result)){
|
316
|
+
|
317
|
+
$data[] = $row;
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
?>
|
326
|
+
|
327
|
+
<!DOCTYPE html>
|
328
|
+
|
329
|
+
<html lang="ja">
|
330
|
+
|
331
|
+
<head>
|
332
|
+
|
333
|
+
<meta charset="UTF-8">
|
334
|
+
|
335
|
+
<title></title>
|
336
|
+
|
337
|
+
</head>
|
338
|
+
|
339
|
+
<body>
|
340
|
+
|
341
|
+
<h1>ひとこと掲示板</h1>
|
342
|
+
|
343
|
+
<form method="post">
|
344
|
+
|
345
|
+
<?php if (count($errors) > 0) { ?>
|
346
|
+
|
347
|
+
<ul>
|
348
|
+
|
349
|
+
<?php foreach ($errors as $value){ ?>
|
350
|
+
|
351
|
+
<li><?php echo htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); ?></li>
|
352
|
+
|
353
|
+
<?php } ?>
|
354
|
+
|
355
|
+
</ul>
|
356
|
+
|
357
|
+
<?php } ?>
|
358
|
+
|
359
|
+
<p>名前 :
|
360
|
+
|
361
|
+
<input type="text" name="name">
|
362
|
+
|
363
|
+
ひとこと :
|
364
|
+
|
365
|
+
<input type="text" name="comment" size="60">
|
366
|
+
|
367
|
+
<input type="submit" value="送信">
|
368
|
+
|
369
|
+
</p>
|
370
|
+
|
371
|
+
</form>
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
<ul>
|
376
|
+
|
377
|
+
<?php if(!empty($data)){ ?>
|
378
|
+
|
379
|
+
<?php foreach ($data as $values) { ?>
|
380
|
+
|
381
|
+
<li>
|
382
|
+
|
383
|
+
<?php echo htmlspecialchars($values['name'],ENT_QUOTES,'UTF-8'); ?>
|
384
|
+
|
385
|
+
<?php echo htmlspecialchars($values['comment'],ENT_QUOTES,'UTF-8'); ?>
|
386
|
+
|
387
|
+
<?php echo htmlspecialchars($values['date'],ENT_QUOTES,'UTF-8'); ?>
|
388
|
+
|
389
|
+
</li>
|
390
|
+
|
391
|
+
<?php } ?>
|
392
|
+
|
393
|
+
<?php } ?>
|
394
|
+
|
395
|
+
</ul>
|
396
|
+
|
397
|
+
</body>
|
398
|
+
|
399
|
+
</html>
|
400
|
+
|
401
|
+
```
|