Q&A
前提
ここに質問の内容を詳しく書いてください。
現在PHPにてデータベースからインサートしたデータをテーブルに表示し、編集・削除をする機能の実装に取り組んでいます。
完成図は添付画像のようになっております。
Viewsフォルダにcontact.php, confirm.php, complete.php, update.php
ControllersフォルダにContactControl.php
ModelsフォルダにDb.phpをそれぞれ作成しております。
実現したいこと
・contact.php内のテーブルに実装されている編集ボタンを押下するとupdate.phpに遷移し、クリックした箇所のデータをそれぞれ表示させたい。
・可能であれば、編集ボタンをクリックした際に、遷移先にてPOSTとしてデータを受け取りたい。
発生している問題・エラーメッセージ
contact.php内にて編集ボタンを押下すると遷移先で添付画像のような文字が入力されてしまう。
該当のソースコード
update.php
1<?php 2ini_set('display_errors', "On"); 3session_start(); 4 5// if ($_SERVER["REQUEST_METHOD"] != "POST") { 6// header("Location: contact.php"); 7// exit(); 8// } 9 10// if ($id == '') { 11// header("Location:./contact.php"); 12// exit; 13// } 14 15 16require_once(ROOT_PATH .'Controllers/ContactControl.php'); 17require_once(ROOT_PATH .'Models/Db.php'); 18require_once(ROOT_PATH .'./database.php'); 19$showData = new UpdateData(); 20$showData->dbUpdate(); 21$errors = new Validate(); 22 23// $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8'; 24// $user = 'root'; 25// $password = 'root'; 26// try { 27// $dbh = new PDO($dsn, $user, $password); 28// $stmt = $dbh->prepare('SELECT * FROM contacts'); 29// $stmt->bindParam(":id", $id); 30// $stmt->execute(); 31// $dbCulum = $stmt->fetchAll(PDO::FETCH_ASSOC); 32 33// echo '接続完了'; 34// // $this->dbCulum = $dbCulum; 35// // return $dbCulum ; 36// } catch (PDOException $e) { 37// echo "接続失敗: " . $e->getMessage() . "\n"; 38// exit(); 39// } finally { 40// $dbh = null; 41// } 42 43 44?> 45 46<!DOCTYPE html> 47<html lang="ja"> 48<head> 49 <meta charset="utf-8"> 50 <title>データ更新画面</title> 51 <link rel="stylesheet" href="../public/css/style.css"> 52</head> 53<body> 54 <div class="container"> 55 <h1 class="contact_title">更新画面</h1> 56 <div class="errorMessate"> 57 <?php $errors->checkUpdate();?> 58 </div> 59 <form action="./update.php" method="post" name="form"> 60 <input type="hidden" name="id" value="<?php echo $dbCulum[0]['id']; ?>"> 61 <table class="contact-table"> 62 <tr> 63 <th class="contact-item">氏名</th> 64 <td class="contact-body"> 65 <input type="text" name="fullname" class="form-text" value="<?php echo $dbCulum[0]['fullname']; ?>"/> 66 </td> 67 </tr> 68 <tr> 69 <th class="contact-item">フリガナ</th> 70 <td class="contact-body"> 71 <input type="text" name="furigana" class="form-text" value="<?php echo $dbCulum[0]['kana']; ?>"/> 72 </td> 73 </tr> 74 <tr> 75 <th class="contact-item">電話番号</th> 76 <td class="contact-body"> 77 <input type="tell" name="tellNumber" class="form-text" value="<?php echo $dbCulum[0]['tel']; ?>"/> 78 </td> 79 </tr> 80 <tr> 81 <th class="contact-item">メールアドレス</th> 82 <td class="contact-body"> 83 <input type="email" name="email" class="form-text" value="<?php echo $dbCulum[0]['email']; ?>"/> 84 </td> 85 </tr> 86 <tr> 87 <th class="contact-item">お問い合わせ内容</th> 88 <td class="contact-body"> 89 <textarea cols="40" rows="8" name="message"><?php echo $dbCulum[0]['body']; ?></textarea> 90 </td> 91 </tr> 92 </table> 93 <div class="confirmMessage"> 94 <p>上記の内容でよろしいですか?</p> 95 </div> 96 <input type="submit" name="cansel" value="キャンセル" onClick="form.action='contact.php';return true"> 97 <input type="submit" name="update" value="更新"> 98 </form> 99 100 <div> 101</body> 102
Db.php
1<?php 2require_once(ROOT_PATH .'./database.php'); 3 4class RegisterDb 5{ 6 public function submitDb() 7 { 8 $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8'; 9 $user = 'root'; 10 $password = 'root'; 11 try { 12 $dbh = new PDO($dsn, $user, $password); 13 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 14 $dbh->beginTransaction(); 15 16 // $fullname = $_SESSION['fullname']; 17 // $furigana = $_SESSION['furigana']; 18 // $tellNumber = $_SESSION['tellNumber']; 19 // $email = $_SESSION['email']; 20 // $message = $_SESSION['message']; 21 22 $fullname = $_POST['fullname']; 23 $kana = $_POST['furigana']; 24 $tel = $_POST['tellNumber']; 25 $email = $_POST['email']; 26 $body = $_POST['message']; 27 28 $sql = "INSERT INTO contacts (fullname, kana, tel, email, body) VALUES (:fullname, :furigana, :tellNumber, :email, :message)"; 29 $stmt = $dbh->prepare($sql); 30 $params = array(':fullname' => $fullname, 'furigana' => $kana, 'tellNumber' => $tel, 'email' => $email, 'message' => $body); 31 $stmt->execute($params); 32 $dbh->commit(); 33 } catch (PDOException $e) { 34 echo "接続失敗: " . $e->getMessage() . "\n"; 35 exit(); 36 } 37 } 38} 39 40class DbData 41{ 42 public $contactsData; 43 44 public function getData() 45 { 46 $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8'; 47 $user = 'root'; 48 $password = 'root'; 49 try { 50 $dbh = new PDO($dsn, $user, $password); 51 $stmt = $dbh->prepare('SELECT * FROM contacts'); 52 $stmt->execute(); 53 $contactsData = $stmt->fetchAll(PDO::FETCH_ASSOC); 54 echo '接続完了'; 55 $this->contactsData = $contactsData; 56 return $contactsData; 57 } catch (PDOException $e) { 58 echo "接続失敗: " . $e->getMessage() . "\n"; 59 exit(); 60 } finally { 61 $dbh = null; 62 } 63 } 64} 65 66class UpdateData 67{ 68 public $dbCulum; 69 70 public function dbUpdate() 71 { 72 $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8'; 73 $user = 'root'; 74 $password = 'root'; 75 $id = $_GET['id']; 76 try { 77 $dbh = new PDO($dsn, $user, $password); 78 $stmt = $dbh->prepare('SELECT * FROM contacts'); 79 $stmt->bindParam(":id", $id); 80 $stmt->execute(); 81 $dbCulum = $stmt->fetchAll(PDO::FETCH_ASSOC); 82 83 echo '接続完了'; 84 $this->dbCulum = $dbCulum; 85 return $dbCulum; 86 } catch (PDOException $e) { 87 echo "接続失敗: " . $e->getMessage() . "\n"; 88 exit(); 89 } finally { 90 $dbh = null; 91 } 92 } 93}
試したこと
下記のようにupdate.phpに入力されているinputタグのvalue値を変更してみたが解決しませんでした。
echo $dbCulum[0]['fullname']→echo $dbCulum[1]['fullname']
補足情報(FW/ツールのバージョンなど)
環境はMAMPとなっております。
どうかよろしくお願いいたします。
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/01/19 06:08