###前提・実現したいこと
PHPで編集機能を実装しています。一覧画面(①)、変更画面(②),更新完了画面(③)、のように遷移させていのですが、(③)のところでつまずいています。
①
②
③
###発生している問題・エラーメッセージ
エラーのメッセージは発生しておりません。現状は下記の画像のような感じです。(③)でつまづいております。上記の③の画像のようにしたいです。
①
②
③
エラーのメッセージの取得に関しては下記のように記述しましたが出てきませんでした。なので恐らく②→③へのデータの受け渡しが問題なのかのと思っています。
update3.phpの下記のコードが画像に反映されているので受け渡しが問題なのかなと思いました。
update3.phpのif文の一部
php
1if(empty($_POST)) { 2 echo "<a href='update1.php'>update1.php</a>←こちらのページからどうぞ"; 3 exit();
エラー文取得のコード
<?php ini_set("display_errors", On); error_reporting(E_ALL);
###該当のソースコード
①一覧画面 update1.php
php
1<?php 2 3header("Content-type: text/html; charset=utf-8"); 4 5require_once("index_db.php"); 6 7 8$sql = "SELECT id, rank, nation, food, reason FROM country"; 9$statement = $pdo->query($sql); 10 11// 結果の取得 12$country = array(); 13foreach ($statement as $row) { 14 $country[] = $row; 15} 16var_dump($country); 17 18// データベース切断 19$pdo = null; 20 21?> 22 23<!DOCTYPE html> 24<html> 25<head> 26<title>国一覧</title> 27</head> 28<body> 29<h1>国一覧</h1> 30 31<table border='1'> 32 <tr><td>id</td><td>順位</td><td>国名</td><td>食べたい食べ物</td><td>理由</td></tr> 33 34<?php 35 foreach($country as $row){ 36 ?> 37 38 39 40 <tr> 41 <td><?=$row['id']?></td> 42 <td><?=htmlspecialchars($row['rank'], ENT_QUOTES, 'UTF-8')?></td> 43 <td><?=htmlspecialchars($row['nation'], ENT_QUOTES, 'UTF-8')?></td> 44 <td><?=htmlspecialchars($row['food'], ENT_QUOTES, 'UTF-8')?></td> 45 <td><?=htmlspecialchars($row['reason'], ENT_QUOTES, 'UTF-8')?></td> 46 <td> 47 <form action="update2.php" method="post"> 48 <input type="submit" value="編集"> 49 <input type="hidden" name="id" value="<?=$row['id']?>"> 50 <input type="hidden" name="rank" value="<?=$row['rank']?>"> 51 <input type="hidden" name="nation" value="<?=$row['nation']?>"> 52 <input type="hidden" name="food" value="<?=$row['food']?>"> 53 <input type="hidden" name="reason" value="<?=$row['reason']?>"> 54 </form> 55 </td> 56</tr> 57 58 59 60 <?php 61 } 62 ?> 63 64</table> 65 66</body> 67</html> 68
②変更画面 update2.php
php
1<?php 2ini_set("display_errors", On); 3error_reporting(E_ALL); 4 5header("content-type: text/html; charset=utf-8"); 6require_once("index_db.php"); 7 8 9try { 10 11 if(empty($_POST)) { 12 echo "<a href='update1.php'>update1.php</a>←こちらのページからどうぞ"; 13 exit(); 14 }else { 15 if (!isset($_POST['id']) || !is_numeric($_POST['id']) ){ 16 echo "idエラー"; 17 exit(); 18 }else { 19 //プリペアドスタートメント 20 $stmt = $pdo->prepare('select * from country where id=?, rank=?, nation=?, food=?, reason=?'); 21 if ($stmt) { 22 //プレースホルダへ実際の値を設定する 23 $stmt->bindParam(1, $id); 24 $stmt->bindParam(2, $rank); 25 $stmt->bindParam(3, $nation); 26 $stmt->bindParam(4, $food); 27 $stmt->bindParam(5, $reason); 28 $id = $_POST['id']; 29 $rank = $_POST['rank']; 30 $nation = $_POST['nation']; 31 $food = $_POST['food']; 32 $reason = $_POST['reason']; 33 34 //クエリの実行 35 $stmt->execute(); 36 37 //結果変数のバインド 38 $result = $stmt->fetch(); 39 40 41 //値の取得 42 $stmt->fetch(); 43 44 //ステートメント切断 45 $pdo = null; 46 } 47 } 48 } 49} catch (Exception $e){ 50 echo 'error'; 51 echo('エラーが発生しました。:'.$e->getMessage()); 52 exit; 53} 54 55 56?> 57 58<iDOCTYPE html> 59 <!DOCTYPE html> 60 <html> 61 62<head> 63 <title>変更画面</title> 64</head> 65 66<body> 67 68<p>変更してください</p> 69 70<table border=1> 71 72<form action="update3.php"> 73 <tr> 74 <th>順位</th> 75 <th>国名</th> 76 <th>食べたい食べ物</th> 77 <th>理由</th> 78 </tr> 79 80 <tr> 81 <td> <input type="hidden" name="id" value="<?=$id?>"></td> 82 <td><input type="text" name="rank" value="<?=htmlspecialchars($rank, ENT_QUOTES, 'UTF-8')?>"></td> 83 <td><input type="text" name="nation" value="<?=htmlspecialchars($nation, ENT_QUOTES, 'UTF-8')?>"></td> 84 <td><input type="text" name="food" value="<?=htmlspecialchars($food, ENT_QUOTES, 'UTF-8')?>"></td> 85 <td> <input type="text" name="reason" value="<?=htmlspecialchars($reason, ENT_QUOTES, 'UTF-8')?>"></td> 86 <td><input type="submit" value="変更する"></td> 87 </tr> 88 89 90 91</form> 92 93</table> 94 95</body> 96 97 </html> 98
③更新完了画面 update3.php
php
1<?php 2ini_set("display_errors", On); 3error_reporting(E_ALL); 4 5header("content-type: text/html; charset=utf-8"); 6require_once("index_db.php"); 7 8try { 9 10 if(empty($_POST)) { 11 echo "<a href='update1.php'>update1.php</a>←こちらのページからどうぞ"; 12 exit(); 13 }else { 14 //名前入力チェック 15 if (!isset($_POST['id']) || $_POST['id'] === "" ){ 16 $errors['id'] = "名前が入力されていません。"; 17 } 18 19 if(count($errors) === 0) { 20 //プリペアドステートメント 21 $stmt = $pdo->prepare("update country set rank=?, nation=?, food=?, reason=? where id=?,"); 22 if ($stmt) { 23 //プレースホルダに実際の値を設定する 24 25 $stmt->bindParam(1, $rank, PDO::PARAM_STR); 26 $stmt->bindParam(2, $nation, PDO::PARSM_STR); 27 $stmt->bindParam(3, $food, PDO::PARAM_STR); 28 $stmt->bindParam(4, $reason, PDO::PARAM_STR); 29 $stmt->bindParam(5, $id, PDO::PARAM_STR); 30 31 32 $id = $_POST['id']; 33 $rank = $_POST['rank']; 34 $nation = $_POST['nation']; 35 $food = $_POST['food']; 36 $reason = $_POST['reason']; 37 38 //クエリ実行 39 $stmt->execute(); 40 //ステートメント切断 41 $pdo = null; 42 43 } 44 } 45 } 46}catch (Exception $e) { 47 echo 'error'; 48 echo ('エラーが発生しました。:'.$e->getMessage()); 49 exit; 50} 51 ?> 52 53 <!DOCTYPE html> 54<html> 55 <head> 56 <title>変更画面</title> 57 </head> 58 <body> 59 <h1>変更画面</h1> 60 61 <?php if (count($errors) === 0): ?> 62 <p>変更完了しました。</p> 63 <?php elseif(count($errors) > 0): ?> 64 <?php 65 foreach($errors as $value){ 66 echo "<p>".$value."</p>"; 67 } 68 ?> 69 <?php endif; ?> 70 71 72 </body> 73 </head> 74 75 76 </html> 77 78
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/02 06:08
2017/09/02 06:15
2017/09/02 06:19
2017/09/02 06:49
2017/09/02 07:27
2017/09/02 07:39
2017/09/02 07:53
2017/09/02 08:02
2017/09/02 08:34
2017/09/02 08:34
2017/09/02 09:19