###前提・実現したいこと
自分なりに編集機能のコーディングをしましたが上手く機能しません。
下記の画像の様な編集をしたいです。(順位、国名、食べたい食べ物、理由)
どなたかサンプルコードをいただけないでしょうか。
###ソースコード
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="#">編集</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="#">編集</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="#">編集</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="#">編集</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="#">編集</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
変更画面
php
1<?php 2 3header("Content-type: text/html; charset=utf-8"); 4 5require_once("index_db.php"); 6 7 8if(empty($_POST)) { 9 echo "<a href='update1.php'>update1.php</a>←こちらのページからどうぞ"; 10 exit(); 11}else{ 12 if (!isset($_POST['id']) || !is_numeric($_POST['id']) ){ 13 echo "IDエラー"; 14 exit(); 15 }else{ 16 //プリペアドステートメント 17 $stmt = $mysqli->prepare("SELECT * FROM member WHERE id=?"); 18 if ($stmt) { 19 //プレースホルダへ実際の値を設定する 20 $stmt->bind_param('i', $id); 21 $id = $_POST['id']; 22 23 //クエリ実行 24 $stmt->execute(); 25 26 //結果変数のバインド 27 $stmt->bind_result($id,$nation); 28 // 値の取得 29 $stmt->fetch(); 30 31 //ステートメント切断 32 $stmt->close(); 33 }else{ 34 echo $mysqli->errno . $mysqli->error; 35 } 36 } 37} 38 39// データベース切断 40$pdo = null; 41 42?> 43 44<!DOCTYPE html> 45<html> 46<head> 47<title>変更画面</title> 48</head> 49<body> 50<h1>変更画面</h1> 51 52<p>名前を変更して下さい。</p> 53<form action="update3.php" method="post"> 54<input type="text" name="name" value="<?=htmlspecialchars($name, ENT_QUOTES, 'UTF-8')?>"> 55<input type="hidden" name="id" value="<?=$id?>"> 56<input type="submit" value="変更する"> 57</form> 58 59</body> 60</html> 61
変更完了画面
php
1<?php 2 3header("Content-type: text/html; charset=utf-8"); 4 5require_once(“index_db.php"); 6 7 8if(empty($_POST)) { 9 echo "<a href='update1.php'>update1.php</a>←こちらのページからどうぞ"; 10 exit(); 11}else{ 12 //名前入力チェック 13 if (!isset($_POST['name']) || $_POST['name'] === "" ){ 14 $errors['name'] = "名前が入力されていません。"; 15 } 16 17 if(count($errors) === 0){ 18 //プリペアドステートメント 19 $stmt = $mysqli->prepare("UPDATE nation SET nation=? WHERE id=?"); 20 if ($stmt) { 21 //プレースホルダへ実際の値を設定する 22 $stmt->bind_param('si', $nation, $id); 23 $name = $_POST[‘nation’]; 24 $id = $_POST['id']; 25 26 //クエリ実行 27 $stmt->execute(); 28 //ステートメント切断 29 $stmt->close(); 30 }else{ 31 echo $mysqli->errno . $mysqli->error; 32 } 33 } 34} 35 36// データベース切断 37$mysqli->close(); 38 39?> 40 41<!DOCTYPE html> 42<html> 43<head> 44<title>変更画面</title> 45</head> 46<body> 47<h1>変更画面</h1> 48 49<?php if (count($errors) === 0): ?> 50<p>変更完了しました。</p> 51<?php elseif(count($errors) > 0): ?> 52<?php 53foreach($errors as $value){ 54 echo "<p>".$value."</p>"; 55} 56?> 57<?php endif; ?> 58 59</body> 60</html>
###補足情報(言語/FW/ツール等のバージョンなど)
PHP 5.6.30,MyCentOS,vagrant,mysql Ver 14.14
回答1件
あなたの回答
tips
プレビュー