###前提・実現したいこと
PHPで編集機能を実装しており、編集完了までを目標にコーディングをしています。
画像①→②→③のように行っていきたいのですが画像①の部分でつまずいています。
①
②
③
###発生している問題・エラーメッセージ
下記の画像から
編集ボタンを押すと下記の画像になります。
PHPでデータは持ってこれていますが、画像①のようにid、name、名前を変更するの下のデータの部分が表示されません。
エラーメッセージは出てはいないですが、update1.phpのHTMLの中のforeach文以下が問題だと思いご質問させていだだきました。
###該当のソースコード
トップページ index.html
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 5<meta charset="UTF-8"> 6<title>My Web Site</title> 7<link rel="stylesheet" href="style.css"> 8<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"> 9 10 </script> 11 12</head> 13<body> 14 15 16<header> 17 18 <div class="header-left"> 19 <img class="logo" src="https://www.fastpic.jp/images.php?file=3519571152.jpg" alt="ロゴ画像"> 20 </div> 21 22<div class="header-right"> 23 <a href="http://onepiece-naruto.com/blog-category-12.html" class="login" target="_blank"><p class="login1">ログイン</p></a> 24</div> 25 26 27</header> 28 29 30<div class="main"> 31 32 <h1>行ってみたい<span>国</span>一覧</h1> 33 <form class="research" action="search.php" method="post"> 34 <input class="research" size="40" type="text" name="nation" value="入力してください"> 35 <input class="research" type="submit" name="research" value="検索"><br> 36 <a href="index2.html"><input class="research" type="button" name="btn" value="新規登録"></a> 37 </form> 38 39 40</div> 41 42 43<div class="wrapper"> 44 45 46 47 <table border="1"> 48 49 <tr> 50 <th class="head">順位</th> 51 <th class="head">国名</th> 52 <th class="head">食べたい食べ物</th> 53 <th class="head">理由</th> 54 <th class="head"></th> 55 </tr> 56 57 58 <tr> 59 <td>1位</td> 60 <td>スペイン</td> 61 <td>パエリア</td> 62 <td>サッカーを見たいから</td> 63 <td><a href="update1.php">編集</a> <a href="#">削除</a></td> 64 </tr> 65 66 <tr> 67 <td>2位</td> 68 <td>アメリカ</td> 69 <td>グリルロブスター</td> 70 <td>ダンスの文化を知りたいから</td> 71 <td><a href="update1.php">編集</a> <a href="#">削除</a></td> 72 </tr> 73 74 <tr> 75 <td>3位</td> 76 <td>インド</td> 77 <td>カレーライス</td> 78 <td>発展途上の国を間近で見てみたいから</td> 79 <td><a href="update1.php">編集</a> <a href="#">削除</a></td> 80 </tr> 81 82 <tr> 83 <td>4位</td> 84 <td>タイ</td> 85 <td>ガパオ</td> 86 <td>バンコクの発展具合を見てみたいから</td> 87 <td><a href="update1.php">編集</a> <a href="#">削除</a></td> 88 </tr> 89 90 <tr> 91 <td>5位</td> 92 <td>フィリピン</td> 93 <td>アドボ </td> 94 <td>友達が楽しそうに働いているから</td> 95 <td><a href="update1.php">編集</a> <a href="#">削除</a></td> 96 </tr> 97 98 99 100 </table> 101 102</div> 103 104 105 106 107 108 109<footer> 110 111 112<div class="footer-left"> 113<img src="https://www.fastpic.jp/images.php?file=3519571152.jpg" alt="ロゴ画像"> 114<p>© travel enjoy.</p> 115</div> 116 117<div class="footer-list"> 118 <ul> 119 <li><a href="https://www.airbnb.jp/">利用規約</a></li> 120 <li><a href="https://www.uber.com/ja-JP/">プライバシー</a></li> 121 <li><a href="https://tabelog.com/tokyo/A1307/A130701/13191934/">アクセシビリティ</a></li> 122 </ul> 123</div> 124 125 126 127</footer> 128 129 <script src="script.js"> 130 131 </script> 132 133 134</body> 135</html> 136
編集ボタンを押したページ 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 35foreach($rows as $row){ 36 ?> 37 38 39 40 <tr> 41 <form action="update2.php" method="post"> 42 <td><?=$row['id']?></td> 43 <td><?=htmlspecialchars($row['rank'],ENT_QUOTES,'UTF-8')?></td> 44 <td><?=htmlspecialchars($row['nation'],ENT_QUOTES,'UTF-8')?></td> 45 <td><?=htmlspecialchars($row['food'],ENT_QUOTES,'UTF-8')?></td> 46 <td><?=htmlspecialchars($row['reason'],ENT_QUOTES,'UTF-8')?></td> 47 <input type="submit" value="変更する"> 48 </form> 49 </tr> 50 51 52 53 <?php 54 } 55 ?> 56 57</table> 58 59</body> 60</html> 61
データベースに繋げるための専用のsql文
php
1<?php 2//DBにMysql、データベース名・testを指定。 3$dsn = 'mysql:dbname=uriage;host=localhost;charset=utf8'; 4 5//DBに接続するためのユーザー名・パスワードを設定 6$user = 'andrew'; 7$password = 'andrew135'; 8$dbName = "uriage"; 9 10try { 11 12 13 14//データーベースに接続 15 $pdo = new PDO($dsn, $user, $password); 16 17 //ここに処理を記載 18 19 20 21} 22 //エラー内容表示 23 catch (Exception $e) { 24 print "エラー!: " . $e->getMessage() . "<br/>"; 25 die(); 26} 27?> 28
###補足情報(言語/FW/ツール等のバージョンなど)
PHP 5.6.30,mysql Ver 14.14,Vagrant,Virtualbox,MyCentOs
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/22 11:37