質問編集履歴

3

酒精コードの追加

2017/12/16 06:11

投稿

dog57
dog57

スコア131

test CHANGED
File without changes
test CHANGED
@@ -30,117 +30,213 @@
30
30
 
31
31
 
32
32
 
33
+
34
+
35
+
36
+
33
- index.php
37
+ update.php
38
+
39
+
34
40
 
35
41
  ```php
36
42
 
43
+ <?php
44
+
45
+ ini_set("display_errors", 1);
46
+
47
+ error_reporting(E_ALL);
48
+
49
+
50
+
51
+ session_start();
52
+
53
+
54
+
55
+ // 文字化け対策
56
+
57
+ header("Content-type: text/html; charset=UTF-8");
58
+
59
+
60
+
61
+ function h($string) {
62
+
63
+ return htmlspecialchars($string, ENT_QUOTES, "UTF-8");
64
+
65
+ }
66
+
67
+
68
+
69
+ require_once(__DIR__. "/db.php");
70
+
71
+
72
+
73
+ $_SESSION["id"] = filter_input(INPUT_POST, 'id');
74
+
75
+ $_SESSION["rank"] = filter_input(INPUT_POST, 'rank');
76
+
77
+ $_SESSION["nation"] = filter_input(INPUT_POST, 'nation');
78
+
79
+ $_SESSION["food"] = filter_input(INPUT_POST, 'food');
80
+
81
+ $_SESSION["reason"] = filter_input(INPUT_POST, 'reason');
82
+
83
+
84
+
85
+ $id = $_SESSION["id"];
86
+
87
+ $rank = $_SESSION["rank"];
88
+
89
+ $nation = $_SESSION["nation"];
90
+
91
+ $food = $_SESSION["food"];
92
+
93
+ $reason = $_SESSION["reason"];
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+ ?>
104
+
105
+
106
+
107
+ <!DOCTYPE html>
108
+
109
+ <html lang="ja">
110
+
111
+
112
+
113
+ <head>
114
+
115
+ <meta charset="utf-8">
116
+
117
+ <title>編集画面</title>
118
+
119
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
120
+
121
+ <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
122
+
123
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
124
+
125
+ <link href="org.css" rel="stylesheet" media="screen">
126
+
127
+ </head>
128
+
129
+
130
+
131
+ <body>
132
+
133
+
134
+
135
+ <!-- エラー表示 -->
136
+
137
+
138
+
139
+
140
+
141
+
142
+
37
143
  <!-- テーブル(表) -->
38
144
 
39
- <div class="table-responsive" style="margin-top:60px;">
40
-
41
- <table id="table1" class="table table-hover">
42
-
43
- <!-- ここは基本記述 -->
44
-
45
- <thead>
46
-
47
- <tr>
48
-
49
- <th>順位</th>
50
-
51
- <th>国名</th>
52
-
53
- <th>食べたい食べ物</th>
54
-
55
- <th>理由</th>
56
-
57
- <th></th>
58
-
59
- </tr>
60
-
61
- </thead>
62
-
63
-
64
-
65
- <?php foreach ($country as $row) {
66
-
67
- ?>
68
-
69
-
70
-
71
- <tbody>
72
-
73
-
74
-
75
- <tr>
76
-
77
- <td><?php echo h($row["rank"]); ?></td>
78
-
79
- <td><?php echo h($row["nation"]); ?></td>
80
-
81
- <td><?php echo h($row["food"]); ?></td>
82
-
83
- <td><?php echo h($row["reason"]); ?></td>
84
-
85
-
86
-
87
- <form action="update.php" method="post" name="update">
88
-
89
- <td>
90
-
91
- <button type="submit" class="btn btn-xs btn-primary">編集</button>
92
-
93
- <button type="submit" class="btn btn-xs btn-danger">削除</button>
94
-
95
- </td>
96
-
97
- <input type="hidden" name="id" value="<?php echo h($row["id"]); ?>">
98
-
99
- <input type="hidden" name="rank" value="<?php echo h($row["rank"]); ?>">
100
-
101
- <input type="hidden" name="nation" value="<?php echo h($row["nation"]); ?>">
102
-
103
- <input type="hidden" name="food" value="<?php echo h($row["food"]); ?>">
104
-
105
- <input type="hidden" name="reason" value="<?php echo h($row["reason"]); ?>">
106
-
107
- </form>
108
-
109
-
110
-
111
- </tr>
112
-
113
-
114
-
115
- </tbody>
116
-
117
-
118
-
119
-
120
-
121
- <?php
122
-
123
- }
124
-
125
- ?>
126
-
127
-
128
-
129
- </form>
130
-
131
- </table>
132
-
133
-
134
-
135
- </div>
145
+ <div class="table-responsive" style="margin-top:60px;">
146
+
147
+ <table id="table1" class="table table-hover">
148
+
149
+ <form action="update_end.php" method="post">
150
+
151
+ <!-- ここは基本記述 -->
152
+
153
+ <thead>
154
+
155
+ <tr>
156
+
157
+ <th>順位</th>
158
+
159
+ <th>国名</th>
160
+
161
+ <th>食べたい食べ物</th>
162
+
163
+ <th>理由</th>
164
+
165
+ <th></th>
166
+
167
+ </tr>
168
+
169
+ </thead>
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+ <tbody>
178
+
179
+
180
+
181
+ <tr>
182
+
183
+ <input type="hidden" name="id" value="<?php echo h($id); ?>">
184
+
185
+ <td><input type="text" name="rank" value="<?php echo h($rank); ?>"></td>
186
+
187
+ <td><input type="text" name="nation" value="<?php echo h($nation); ?>"></td>
188
+
189
+ <td><input type="text" name="food" value="<?php echo h($food); ?>"</td>
190
+
191
+ <td><input type="text" name="reason" value="<?php echo h($reason); ?>"></td>
192
+
193
+ <td>
194
+
195
+ <button type="submit" class="btn btn-primary btn-xs">変更する</button>
196
+
197
+ </td>
198
+
199
+ </tr>
200
+
201
+
202
+
203
+ </tbody>
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+ </form>
214
+
215
+ </table>
216
+
217
+ </div>
218
+
219
+
220
+
221
+
222
+
223
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
224
+
225
+ <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
226
+
227
+ </body>
228
+
229
+
230
+
231
+ </html>
232
+
233
+
136
234
 
137
235
  ```
138
236
 
139
237
 
140
238
 
141
- update.php
239
+ update_end.php
142
-
143
-
144
240
 
145
241
  ```php
146
242
 
@@ -160,19 +256,15 @@
160
256
 
161
257
  header("Content-type: text/html; charset=UTF-8");
162
258
 
163
-
259
+ // 初期化
164
-
260
+
165
- function h($string) {
261
+ $errors = array();
166
-
167
- return htmlspecialchars($string, ENT_QUOTES, "UTF-8");
168
-
169
- }
170
262
 
171
263
 
172
264
 
173
265
  require_once(__DIR__. "/db.php");
174
266
 
175
-
267
+ // セッション変数の定義
176
268
 
177
269
  $_SESSION["id"] = filter_input(INPUT_POST, 'id');
178
270
 
@@ -184,7 +276,7 @@
184
276
 
185
277
  $_SESSION["reason"] = filter_input(INPUT_POST, 'reason');
186
278
 
187
-
279
+ // 変数の定義
188
280
 
189
281
  $id = $_SESSION["id"];
190
282
 
@@ -200,7 +292,129 @@
200
292
 
201
293
 
202
294
 
203
-
295
+ // 入力チェック
296
+
297
+ if (empty($_SESSION["rank"])) {
298
+
299
+ $errors["rank"] = "順位が入力されていません。";
300
+
301
+ }
302
+
303
+
304
+
305
+ if (empty($_SESSION["nation"])) {
306
+
307
+ $errors["nation"] = "国名が入力されていません。";
308
+
309
+ }
310
+
311
+
312
+
313
+ if (empty($_SESSION["food"])) {
314
+
315
+ $errors["food"] = "食べ物が入力されていません。";
316
+
317
+ }
318
+
319
+
320
+
321
+ if (empty($_SESSION["reason"])) {
322
+
323
+ $errors["reason"] = "理由が入力されていません。";
324
+
325
+ }
326
+
327
+
328
+
329
+ // 数値チェック
330
+
331
+ if (!preg_match('/^[0-9]*/', $rank) && !empty($_SESSION["rank"])) {
332
+
333
+ $errors["rank"] = "順位を入力してください。";
334
+
335
+ }
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+ if (count($errors) === 0) {
344
+
345
+
346
+
347
+ try {
348
+
349
+
350
+
351
+ // プリペアドステートメント
352
+
353
+ $statement = $dbh->prepare("UPDATE country SET id = :id, rank = :rank, nation = :nation, food = :food, reason = :reason");
354
+
355
+
356
+
357
+ if ($statement) {
358
+
359
+ // プレースホルダへ実際の値を設定する
360
+
361
+ $statement->bindValue(':id', $rank, PDO::PARAM_STR);
362
+
363
+ $statement->bindValue(':rank', $rank, PDO::PARAM_STR);
364
+
365
+ $statement->bindValue(':nation', $nation, PDO::PARAM_STR);
366
+
367
+ $statement->bindValue(':food', $food, PDO::PARAM_STR);
368
+
369
+ $statement->bindValue(':reason', $reason, PDO::PARAM_STR);
370
+
371
+
372
+
373
+ $_SESSION["id"] = filter_input(INPUT_POST, $id);
374
+
375
+ $_SESSION["rank"] = filter_input(INPUT_POST, $rank);
376
+
377
+ $_SESSION["nation"] = filter_input(INPUT_POST, $nation);
378
+
379
+ $_SESSION["food"] = filter_input(INPUT_POST, $food);
380
+
381
+ $_SESSION["reason"] = filter_input(INPUT_POST, $reason);
382
+
383
+
384
+
385
+ // クエリ実行
386
+
387
+ $statement->execute();
388
+
389
+
390
+
391
+ // データベース切断
392
+
393
+ $dbh = null;
394
+
395
+ }
396
+
397
+ } catch (PDOException $e) {
398
+
399
+ print('Error:' .$e->getMessage());
400
+
401
+ $errors["error"] = "データベース接続失敗しました。";
402
+
403
+ }
404
+
405
+
406
+
407
+
408
+
409
+ } elseif (count($errors) > 0) {
410
+
411
+ foreach ($errors as $error) {
412
+
413
+ $_SESSION["errors"] = $errors;
414
+
415
+ }
416
+
417
+ }
204
418
 
205
419
 
206
420
 
@@ -208,6 +422,8 @@
208
422
 
209
423
 
210
424
 
425
+
426
+
211
427
  <!DOCTYPE html>
212
428
 
213
429
  <html lang="ja">
@@ -218,7 +434,7 @@
218
434
 
219
435
  <meta charset="utf-8">
220
436
 
221
- <title>編集画面</title>
437
+ <title>更新完了</title>
222
438
 
223
439
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
224
440
 
@@ -236,95 +452,13 @@
236
452
 
237
453
 
238
454
 
239
- <!-- エラー表示 -->
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
- <!-- テーブル(表) -->
248
-
249
- <div class="table-responsive" style="margin-top:60px;">
250
-
251
- <table id="table1" class="table table-hover">
252
-
253
- <form action="update_end.php" method="post">
254
-
255
- <!-- ここは基本記述 -->
256
-
257
- <thead>
258
-
259
- <tr>
260
-
261
- <th>順位</th>
455
+ <p>更新完了</p>
262
-
263
- <th>国名</th>
456
+
264
-
265
- <th>食べたい食べ物</th>
266
-
267
- <th>理由</th>
268
-
269
- <th></th>
270
-
271
- </tr>
272
-
273
- </thead>
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
- <tbody>
282
-
283
-
284
-
285
- <tr>
286
-
287
- <input type="hidden" name="id" value="<?php echo h($id); ?>">
288
-
289
- <td><input type="text" name="rank" value="<?php echo h($rank); ?>"></td>
290
-
291
- <td><input type="text" name="nation" value="<?php echo h($nation); ?>"></td>
457
+ <a href="index.php"><button type="button" name="singlebutton" class="btn btn-primary" id="singlebutton">戻る</button></a>
292
-
293
- <td><input type="text" name="food" value="<?php echo h($food); ?>"</td>
458
+
294
-
295
- <td><input type="text" name="reason" value="<?php echo h($reason); ?>"></td>
459
+
296
-
297
- <td>
460
+
298
-
299
- <button type="submit" class="btn btn-primary btn-xs">変更する</button>
300
-
301
- </td>
302
-
303
- </tr>
304
-
305
-
306
-
307
- </tbody>
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
- </form>
318
-
319
- </table>
320
-
321
- </div>
322
-
323
-
324
-
325
-
326
-
327
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
461
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
328
462
 
329
463
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
330
464
 
@@ -340,7 +474,15 @@
340
474
 
341
475
 
342
476
 
477
+ ![イメージ説明](aea3837f4c0526da01d37e7fa97c0a81.png)
478
+
479
+ ![イメージ説明](1b98eed2173dec4160d49b89ebcbcf86.png)
480
+
481
+
482
+
483
+
484
+
343
- update_end.php
485
+ 修正後 update_end.php
344
486
 
345
487
  ```php
346
488
 
@@ -432,7 +574,7 @@
432
574
 
433
575
  // 数値チェック
434
576
 
435
- if (!preg_match('/^[0-9]*/', $rank) && !empty($_SESSION["rank"])) {
577
+ if (!preg_match('/^[0-9]*/', $rank) && !empty($_SESSION["rank"])) {
436
578
 
437
579
  $errors["rank"] = "順位を入力してください。";
438
580
 
@@ -452,9 +594,13 @@
452
594
 
453
595
 
454
596
 
597
+ $dbh = new PDO($dsn, $user, $password);
598
+
599
+
600
+
455
601
  // プリペアドステートメント
456
602
 
457
- $statement = $dbh->prepare("UPDATE country SET id = :id, rank = :rank, nation = :nation, food = :food, reason = :reason");
603
+ $statement = $dbh->prepare("UPDATE country SET rank = :rank, nation = :nation, food = :food, reason = :reason WHERE id = :id");
458
604
 
459
605
 
460
606
 
@@ -474,15 +620,17 @@
474
620
 
475
621
 
476
622
 
623
+
624
+
477
- $_SESSION["id"] = filter_input(INPUT_POST, $id);
625
+ $id = filter_input(INPUT_POST, $id);
478
-
626
+
479
- $_SESSION["rank"] = filter_input(INPUT_POST, $rank);
627
+ $rank = filter_input(INPUT_POST, $rank);
480
-
628
+
481
- $_SESSION["nation"] = filter_input(INPUT_POST, $nation);
629
+ $nation = filter_input(INPUT_POST, $nation);
482
-
630
+
483
- $_SESSION["food"] = filter_input(INPUT_POST, $food);
631
+ $food = filter_input(INPUT_POST, $food);
484
-
632
+
485
- $_SESSION["reason"] = filter_input(INPUT_POST, $reason);
633
+ $reason = filter_input(INPUT_POST, $reason);
486
634
 
487
635
 
488
636
 
@@ -575,9 +723,3 @@
575
723
 
576
724
 
577
725
  ```
578
-
579
-
580
-
581
- ![イメージ説明](aea3837f4c0526da01d37e7fa97c0a81.png)
582
-
583
- ![イメージ説明](1b98eed2173dec4160d49b89ebcbcf86.png)

2

画像の追加

2017/12/16 06:11

投稿

dog57
dog57

スコア131

test CHANGED
File without changes
test CHANGED
@@ -579,3 +579,5 @@
579
579
 
580
580
 
581
581
  ![イメージ説明](aea3837f4c0526da01d37e7fa97c0a81.png)
582
+
583
+ ![イメージ説明](1b98eed2173dec4160d49b89ebcbcf86.png)

1

画像の追加

2017/12/16 05:54

投稿

dog57
dog57

スコア131

test CHANGED
File without changes
test CHANGED
@@ -575,3 +575,7 @@
575
575
 
576
576
 
577
577
  ```
578
+
579
+
580
+
581
+ ![イメージ説明](aea3837f4c0526da01d37e7fa97c0a81.png)