質問編集履歴
3
酒精コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,60 +14,8 @@
|
|
14
14
|
・DB情報
|
15
15
|

|
16
16
|
|
17
|
-
index.php
|
18
|
-
```php
|
19
|
-
<!-- テーブル(表) -->
|
20
|
-
<div class="table-responsive" style="margin-top:60px;">
|
21
|
-
<table id="table1" class="table table-hover">
|
22
|
-
<!-- ここは基本記述 -->
|
23
|
-
<thead>
|
24
|
-
<tr>
|
25
|
-
<th>順位</th>
|
26
|
-
<th>国名</th>
|
27
|
-
<th>食べたい食べ物</th>
|
28
|
-
<th>理由</th>
|
29
|
-
<th></th>
|
30
|
-
</tr>
|
31
|
-
</thead>
|
32
17
|
|
33
|
-
<?php foreach ($country as $row) {
|
34
|
-
?>
|
35
18
|
|
36
|
-
<tbody>
|
37
|
-
|
38
|
-
<tr>
|
39
|
-
<td><?php echo h($row["rank"]); ?></td>
|
40
|
-
<td><?php echo h($row["nation"]); ?></td>
|
41
|
-
<td><?php echo h($row["food"]); ?></td>
|
42
|
-
<td><?php echo h($row["reason"]); ?></td>
|
43
|
-
|
44
|
-
<form action="update.php" method="post" name="update">
|
45
|
-
<td>
|
46
|
-
<button type="submit" class="btn btn-xs btn-primary">編集</button>
|
47
|
-
<button type="submit" class="btn btn-xs btn-danger">削除</button>
|
48
|
-
</td>
|
49
|
-
<input type="hidden" name="id" value="<?php echo h($row["id"]); ?>">
|
50
|
-
<input type="hidden" name="rank" value="<?php echo h($row["rank"]); ?>">
|
51
|
-
<input type="hidden" name="nation" value="<?php echo h($row["nation"]); ?>">
|
52
|
-
<input type="hidden" name="food" value="<?php echo h($row["food"]); ?>">
|
53
|
-
<input type="hidden" name="reason" value="<?php echo h($row["reason"]); ?>">
|
54
|
-
</form>
|
55
|
-
|
56
|
-
</tr>
|
57
|
-
|
58
|
-
</tbody>
|
59
|
-
|
60
|
-
|
61
|
-
<?php
|
62
|
-
}
|
63
|
-
?>
|
64
|
-
|
65
|
-
</form>
|
66
|
-
</table>
|
67
|
-
|
68
|
-
</div>
|
69
|
-
```
|
70
|
-
|
71
19
|
update.php
|
72
20
|
|
73
21
|
```php
|
@@ -289,4 +237,127 @@
|
|
289
237
|
```
|
290
238
|
|
291
239
|

|
292
|
-

|
240
|
+

|
241
|
+
|
242
|
+
|
243
|
+
修正後 update_end.php
|
244
|
+
```php
|
245
|
+
<?php
|
246
|
+
ini_set("display_errors", 1);
|
247
|
+
error_reporting(E_ALL);
|
248
|
+
|
249
|
+
session_start();
|
250
|
+
|
251
|
+
// 文字化け対策
|
252
|
+
header("Content-type: text/html; charset=UTF-8");
|
253
|
+
// 初期化
|
254
|
+
$errors = array();
|
255
|
+
|
256
|
+
require_once(__DIR__. "/db.php");
|
257
|
+
// セッション変数の定義
|
258
|
+
$_SESSION["id"] = filter_input(INPUT_POST, 'id');
|
259
|
+
$_SESSION["rank"] = filter_input(INPUT_POST, 'rank');
|
260
|
+
$_SESSION["nation"] = filter_input(INPUT_POST, 'nation');
|
261
|
+
$_SESSION["food"] = filter_input(INPUT_POST, 'food');
|
262
|
+
$_SESSION["reason"] = filter_input(INPUT_POST, 'reason');
|
263
|
+
// 変数の定義
|
264
|
+
$id = $_SESSION["id"];
|
265
|
+
$rank = $_SESSION["rank"];
|
266
|
+
$nation = $_SESSION["nation"];
|
267
|
+
$food = $_SESSION["food"];
|
268
|
+
$reason = $_SESSION["reason"];
|
269
|
+
|
270
|
+
|
271
|
+
// 入力チェック
|
272
|
+
if (empty($_SESSION["rank"])) {
|
273
|
+
$errors["rank"] = "順位が入力されていません。";
|
274
|
+
}
|
275
|
+
|
276
|
+
if (empty($_SESSION["nation"])) {
|
277
|
+
$errors["nation"] = "国名が入力されていません。";
|
278
|
+
}
|
279
|
+
|
280
|
+
if (empty($_SESSION["food"])) {
|
281
|
+
$errors["food"] = "食べ物が入力されていません。";
|
282
|
+
}
|
283
|
+
|
284
|
+
if (empty($_SESSION["reason"])) {
|
285
|
+
$errors["reason"] = "理由が入力されていません。";
|
286
|
+
}
|
287
|
+
|
288
|
+
// 数値チェック
|
289
|
+
if (!preg_match('/^[0-9]*/', $rank) && !empty($_SESSION["rank"])) {
|
290
|
+
$errors["rank"] = "順位を入力してください。";
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
if (count($errors) === 0) {
|
296
|
+
|
297
|
+
try {
|
298
|
+
|
299
|
+
$dbh = new PDO($dsn, $user, $password);
|
300
|
+
|
301
|
+
// プリペアドステートメント
|
302
|
+
$statement = $dbh->prepare("UPDATE country SET rank = :rank, nation = :nation, food = :food, reason = :reason WHERE id = :id");
|
303
|
+
|
304
|
+
if ($statement) {
|
305
|
+
// プレースホルダへ実際の値を設定する
|
306
|
+
$statement->bindValue(':id', $rank, PDO::PARAM_STR);
|
307
|
+
$statement->bindValue(':rank', $rank, PDO::PARAM_STR);
|
308
|
+
$statement->bindValue(':nation', $nation, PDO::PARAM_STR);
|
309
|
+
$statement->bindValue(':food', $food, PDO::PARAM_STR);
|
310
|
+
$statement->bindValue(':reason', $reason, PDO::PARAM_STR);
|
311
|
+
|
312
|
+
|
313
|
+
$id = filter_input(INPUT_POST, $id);
|
314
|
+
$rank = filter_input(INPUT_POST, $rank);
|
315
|
+
$nation = filter_input(INPUT_POST, $nation);
|
316
|
+
$food = filter_input(INPUT_POST, $food);
|
317
|
+
$reason = filter_input(INPUT_POST, $reason);
|
318
|
+
|
319
|
+
// クエリ実行
|
320
|
+
$statement->execute();
|
321
|
+
|
322
|
+
// データベース切断
|
323
|
+
$dbh = null;
|
324
|
+
}
|
325
|
+
} catch (PDOException $e) {
|
326
|
+
print('Error:' .$e->getMessage());
|
327
|
+
$errors["error"] = "データベース接続失敗しました。";
|
328
|
+
}
|
329
|
+
|
330
|
+
|
331
|
+
} elseif (count($errors) > 0) {
|
332
|
+
foreach ($errors as $error) {
|
333
|
+
$_SESSION["errors"] = $errors;
|
334
|
+
}
|
335
|
+
}
|
336
|
+
|
337
|
+
?>
|
338
|
+
|
339
|
+
|
340
|
+
<!DOCTYPE html>
|
341
|
+
<html lang="ja">
|
342
|
+
|
343
|
+
<head>
|
344
|
+
<meta charset="utf-8">
|
345
|
+
<title>更新完了</title>
|
346
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
347
|
+
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
|
348
|
+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
|
349
|
+
<link href="org.css" rel="stylesheet" media="screen">
|
350
|
+
</head>
|
351
|
+
|
352
|
+
<body>
|
353
|
+
|
354
|
+
<p>更新完了</p>
|
355
|
+
<a href="index.php"><button type="button" name="singlebutton" class="btn btn-primary" id="singlebutton">戻る</button></a>
|
356
|
+
|
357
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
|
358
|
+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
|
359
|
+
</body>
|
360
|
+
|
361
|
+
</html>
|
362
|
+
|
363
|
+
```
|
2
画像の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -288,4 +288,5 @@
|
|
288
288
|
|
289
289
|
```
|
290
290
|
|
291
|
-

|
291
|
+

|
292
|
+

|
1
画像の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -286,4 +286,6 @@
|
|
286
286
|
|
287
287
|
</html>
|
288
288
|
|
289
|
-
```
|
289
|
+
```
|
290
|
+
|
291
|
+

|