質問編集履歴

2

前職の社外秘の内容が含まれていたため、可能であれば投稿を削除してほしいです。

2024/03/25 09:06

投稿

tomotomo4322
tomotomo4322

スコア7

test CHANGED
File without changes
test CHANGED
@@ -1,243 +1,4 @@
1
1
  ### 前提
2
2
 
3
3
  ここに質問の内容を詳しく書いてください。
4
- 現在PHPにてデータベースからインサートしたデータをテーブルに表示し、編集・削除をする機能の実装に取り組んでいます。
5
- 完成図は添付画像のようになっております。
6
-
7
- Viewsフォルダにcontact.php, confirm.php, complete.php, update.php
8
- ControllersフォルダにContactControl.php
9
- ModelsフォルダにDb.phpをそれぞれ作成しております。
10
-
11
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-01-19/68817641-9e77-43a7-8804-85ec9f59f7e0.png)
12
-
13
- ### 実現したいこと
14
-
15
- ・contact.php内のテーブルに実装されている編集ボタンを押下するとupdate.phpに遷移し、クリックした箇所のデータをそれぞれ表示させたい。
16
-
17
- ・可能であれば、編集ボタンをクリックした際に、遷移先にてPOSTとしてデータを受け取りたい。
18
-
19
-
20
- ### 発生している問題・エラーメッセージ
21
- contact.php内にて編集ボタンを押下すると遷移先で添付画像のような文字が入力されてしまう。
22
-
23
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-01-19/667cebf1-b2f2-45ea-87fc-8600f8774fd6.png)
24
- ```
25
-
26
- ```
27
-
28
- ### 該当のソースコード
29
-
30
-
31
- ```update.php
32
- <?php
33
- ini_set('display_errors', "On");
34
- session_start();
35
-
36
- // if ($_SERVER["REQUEST_METHOD"] != "POST") {
37
- // header("Location: contact.php");
38
- // exit();
39
- // }
40
-
41
- // if ($id == '') {
42
- // header("Location:./contact.php");
43
- // exit;
44
- // }
45
-
46
-
47
- require_once(ROOT_PATH .'Controllers/ContactControl.php');
48
- require_once(ROOT_PATH .'Models/Db.php');
49
- require_once(ROOT_PATH .'./database.php');
50
- $showData = new UpdateData();
51
- $showData->dbUpdate();
52
- $errors = new Validate();
53
-
54
- // $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8';
55
- // $user = 'root';
56
- // $password = 'root';
57
- // try {
58
- // $dbh = new PDO($dsn, $user, $password);
59
- // $stmt = $dbh->prepare('SELECT * FROM contacts');
60
- // $stmt->bindParam(":id", $id);
61
- // $stmt->execute();
62
- // $dbCulum = $stmt->fetchAll(PDO::FETCH_ASSOC);
63
-
64
- // echo '接続完了';
65
- // // $this->dbCulum = $dbCulum;
66
- // // return $dbCulum ;
67
- // } catch (PDOException $e) {
68
- // echo "接続失敗: " . $e->getMessage() . "\n";
69
- // exit();
70
- // } finally {
71
- // $dbh = null;
72
- // }
73
-
74
-
75
- ?>
76
-
77
- <!DOCTYPE html>
78
- <html lang="ja">
79
- <head>
80
- <meta charset="utf-8">
81
- <title>データ更新画面</title>
82
- <link rel="stylesheet" href="../public/css/style.css">
83
- </head>
84
- <body>
85
- <div class="container">
86
- <h1 class="contact_title">更新画面</h1>
87
- <div class="errorMessate">
88
- <?php $errors->checkUpdate();?>
89
- </div>
90
- <form action="./update.php" method="post" name="form">
91
- <input type="hidden" name="id" value="<?php echo $dbCulum[0]['id']; ?>">
92
- <table class="contact-table">
93
- <tr>
94
- <th class="contact-item">氏名</th>
95
- <td class="contact-body">
96
- <input type="text" name="fullname" class="form-text" value="<?php echo $dbCulum[0]['fullname']; ?>"/>
97
- </td>
98
- </tr>
99
- <tr>
100
- <th class="contact-item">フリガナ</th>
101
- <td class="contact-body">
102
- <input type="text" name="furigana" class="form-text" value="<?php echo $dbCulum[0]['kana']; ?>"/>
103
- </td>
104
- </tr>
105
- <tr>
106
- <th class="contact-item">電話番号</th>
107
- <td class="contact-body">
108
- <input type="tell" name="tellNumber" class="form-text" value="<?php echo $dbCulum[0]['tel']; ?>"/>
109
- </td>
110
- </tr>
111
- <tr>
112
- <th class="contact-item">メールアドレス</th>
113
- <td class="contact-body">
114
- <input type="email" name="email" class="form-text" value="<?php echo $dbCulum[0]['email']; ?>"/>
115
- </td>
116
- </tr>
117
- <tr>
118
- <th class="contact-item">お問い合わせ内容</th>
119
- <td class="contact-body">
120
- <textarea cols="40" rows="8" name="message"><?php echo $dbCulum[0]['body']; ?></textarea>
121
- </td>
122
- </tr>
123
- </table>
124
- <div class="confirmMessage">
125
- <p>上記の内容でよろしいですか?</p>
126
- </div>
127
- <input type="submit" name="cansel" value="キャンセル" onClick="form.action='contact.php';return true">
128
- <input type="submit" name="update" value="更新">
129
- </form>
130
-
131
- <div>
132
- </body>
133
-
134
- ```
135
-
136
- ```Db.php
137
- <?php
138
- require_once(ROOT_PATH .'./database.php');
139
-
140
- class RegisterDb
141
- {
142
- public function submitDb()
143
- {
144
- $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8';
145
- $user = 'root';
146
- $password = 'root';
147
- try {
148
- $dbh = new PDO($dsn, $user, $password);
149
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
150
- $dbh->beginTransaction();
151
-
152
- // $fullname = $_SESSION['fullname'];
153
- // $furigana = $_SESSION['furigana'];
154
- // $tellNumber = $_SESSION['tellNumber'];
155
- // $email = $_SESSION['email'];
156
- // $message = $_SESSION['message'];
157
-
158
- $fullname = $_POST['fullname'];
159
- $kana = $_POST['furigana'];
160
- $tel = $_POST['tellNumber'];
161
- $email = $_POST['email'];
162
- $body = $_POST['message'];
163
-
164
- $sql = "INSERT INTO contacts (fullname, kana, tel, email, body) VALUES (:fullname, :furigana, :tellNumber, :email, :message)";
165
- $stmt = $dbh->prepare($sql);
166
- $params = array(':fullname' => $fullname, 'furigana' => $kana, 'tellNumber' => $tel, 'email' => $email, 'message' => $body);
167
- $stmt->execute($params);
168
- $dbh->commit();
169
- } catch (PDOException $e) {
170
- echo "接続失敗: " . $e->getMessage() . "\n";
171
- exit();
4
+ 削除願います
172
- }
173
- }
174
- }
175
-
176
- class DbData
177
- {
178
- public $contactsData;
179
-
180
- public function getData()
181
- {
182
- $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8';
183
- $user = 'root';
184
- $password = 'root';
185
- try {
186
- $dbh = new PDO($dsn, $user, $password);
187
- $stmt = $dbh->prepare('SELECT * FROM contacts');
188
- $stmt->execute();
189
- $contactsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
190
- echo '接続完了';
191
- $this->contactsData = $contactsData;
192
- return $contactsData;
193
- } catch (PDOException $e) {
194
- echo "接続失敗: " . $e->getMessage() . "\n";
195
- exit();
196
- } finally {
197
- $dbh = null;
198
- }
199
- }
200
- }
201
-
202
- class UpdateData
203
- {
204
- public $dbCulum;
205
-
206
- public function dbUpdate()
207
- {
208
- $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8';
209
- $user = 'root';
210
- $password = 'root';
211
- $id = $_GET['id'];
212
- try {
213
- $dbh = new PDO($dsn, $user, $password);
214
- $stmt = $dbh->prepare('SELECT * FROM contacts');
215
- $stmt->bindParam(":id", $id);
216
- $stmt->execute();
217
- $dbCulum = $stmt->fetchAll(PDO::FETCH_ASSOC);
218
-
219
- echo '接続完了';
220
- $this->dbCulum = $dbCulum;
221
- return $dbCulum;
222
- } catch (PDOException $e) {
223
- echo "接続失敗: " . $e->getMessage() . "\n";
224
- exit();
225
- } finally {
226
- $dbh = null;
227
- }
228
- }
229
- }
230
- ```
231
-
232
- ### 試したこと
233
- 下記のようにupdate.phpに入力されているinputタグのvalue値を変更してみたが解決しませんでした。
234
-
235
- echo $dbCulum[0]['fullname']→echo $dbCulum[1]['fullname']
236
-
237
-
238
-
239
- ### 補足情報(FW/ツールのバージョンなど)
240
- 環境はMAMPとなっております。
241
-
242
- どうかよろしくお願いいたします。
243
-

1

文法の修正

2023/01/19 06:10

投稿

tomotomo4322
tomotomo4322

スコア7

test CHANGED
File without changes
test CHANGED
@@ -27,118 +27,12 @@
27
27
 
28
28
  ### 該当のソースコード
29
29
 
30
- ```contact.php
31
- <?php
32
- ini_set('display_errors', "On");
33
- session_start();
34
- header('Expires:-1');
35
- header('Cache-Control:');
36
- header('Pragma:');
37
-
38
- if (empty($_POST)) {
39
- $_SESSION['fullname'] = '';
40
- $_SESSION['furigana'] = '';
41
- $_SESSION['tellNumber'] = '';
42
- $_SESSION['email'] = '';
43
- $_SESSION['message'] = '';
44
- }
45
-
46
- require_once(ROOT_PATH .'Controllers/ContactControl.php');
47
- require_once(ROOT_PATH .'Models/Db.php');
48
- $errors = new Validate;
49
- $showData = new DbData();
50
-
51
-
52
-
53
- ?>
54
-
55
- <!DOCTYPE html>
56
- <html lang="ja">
57
- <head>
58
- <meta charset="utf-8">
59
- <title>お問い合わせフォーム</title>
60
- <link rel="stylesheet" href="../public/css/style.css">
61
- </head>
62
- <body>
63
- <div class="container">
64
- <h1 class="contact_title">お問い合わせ</h1>
65
- <?php
66
- $errors->checkError();
67
- ?>
68
- <form action="./contact.php" method="post" name="form">
69
- <table class="contact-table">
70
- <tr>
71
- <th class="contact-item">氏名</th>
72
- <td class="contact-body">
73
- <input type="text" name="fullname" class="form-text" value="<?php echo $_SESSION['fullname']; ?>"/>
74
- </td>
75
- </tr>
76
- <tr>
77
- <th class="contact-item">フリガナ</th>
78
- <td class="contact-body">
79
- <input type="text" name="furigana" class="form-text" value="<?php echo $_SESSION['furigana']; ?>"/>
80
- </td>
81
- </tr>
82
- <tr>
83
- <th class="contact-item">電話番号</th>
84
- <td class="contact-body">
85
- <input type="tell" name="tellNumber" class="form-text" value="<?php echo $_SESSION['tellNumber']; ?>"/>
86
- </td>
87
- </tr>
88
- <tr>
89
- <th class="contact-item">メールアドレス</th>
90
- <td class="contact-body">
91
- <input type="email" name="email" class="form-text" value="<?php echo $_SESSION['email'];?>"/>
92
- </td>
93
- </tr>
94
- <tr>
95
- <th class="contact-item">お問い合わせ内容</th>
96
- <td class="contact-body">
97
- <textarea cols="40" rows="8" name="message"><?php echo ($_SESSION['message']); ?></textarea>
98
- </td>
99
- </tr>
100
- </table>
101
- <input type="submit" name="check" class="btn" value="確認画面へ">
102
- </form>
103
- <div>
104
- <table class="dataTable">
105
- <tr>
106
- <th>氏名</th>
107
- <th>フリガナ</th>
108
- <th>電話番号</th>
109
- <th>メールアドレス</th>
110
- <th>お問い合わせ内容</th>
111
- </tr>
112
- <?php foreach ($showData->contactsData as $dbCulum) : ?>
113
- <tr>
114
- <td><?php echo $dbCulum['fullname']; ?></td>
115
- <td><?php echo $dbCulum['kana']; ?></td>
116
- <td><?php echo $dbCulum['tel']; ?></td>
117
- <td><?php echo $dbCulum['email']; ?></td>
118
- <td><?php echo $dbCulum['body']; ?></td>
119
- <td>
120
- <button type="button" class="btn" onclick="location.href='update.php?id=<?php echo $dbCulum['id']; ?>'">編集</button>
121
- </td>
122
- <td>
123
- <button type="button" class="btn">削除</button>
124
- </td>
125
- </tr>
126
- <?php endforeach; ?>
127
- </table>
128
- </div>
129
- <div>
130
- </body>
131
-
132
-
133
- ```
134
30
 
135
31
  ```update.php
136
32
  <?php
137
33
  ini_set('display_errors', "On");
138
34
  session_start();
139
35
 
140
- $id = isset($_GET['id']);
141
-
142
36
  // if ($_SERVER["REQUEST_METHOD"] != "POST") {
143
37
  // header("Location: contact.php");
144
38
  // exit();
@@ -149,17 +43,33 @@
149
43
  // exit;
150
44
  // }
151
45
 
152
- // $fullname = htmlspecialchars($_GET['fullname'], ENT_QUOTES);
153
- // $kana = htmlspecialchars($_GET['kana'], ENT_QUOTES);
154
- // $tel = htmlspecialchars($_GET['tel'], ENT_QUOTES);
155
- // $email = htmlspecialchars($_GET['email'], ENT_QUOTES);
156
- // $body = htmlspecialchars($_GET['body'], ENT_QUOTES);
157
-
158
-
159
46
 
160
47
  require_once(ROOT_PATH .'Controllers/ContactControl.php');
161
48
  require_once(ROOT_PATH .'Models/Db.php');
49
+ require_once(ROOT_PATH .'./database.php');
50
+ $showData = new UpdateData();
51
+ $showData->dbUpdate();
162
- $errors = new Validate;
52
+ $errors = new Validate();
53
+
54
+ // $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8';
55
+ // $user = 'root';
56
+ // $password = 'root';
57
+ // try {
58
+ // $dbh = new PDO($dsn, $user, $password);
59
+ // $stmt = $dbh->prepare('SELECT * FROM contacts');
60
+ // $stmt->bindParam(":id", $id);
61
+ // $stmt->execute();
62
+ // $dbCulum = $stmt->fetchAll(PDO::FETCH_ASSOC);
63
+
64
+ // echo '接続完了';
65
+ // // $this->dbCulum = $dbCulum;
66
+ // // return $dbCulum ;
67
+ // } catch (PDOException $e) {
68
+ // echo "接続失敗: " . $e->getMessage() . "\n";
69
+ // exit();
70
+ // } finally {
71
+ // $dbh = null;
72
+ // }
163
73
 
164
74
 
165
75
  ?>
@@ -174,9 +84,9 @@
174
84
  <body>
175
85
  <div class="container">
176
86
  <h1 class="contact_title">更新画面</h1>
177
- <?php
87
+ <div class="errorMessate">
178
- $errors->checkUpdate();
88
+ <?php $errors->checkUpdate();?>
179
- ?>
89
+ </div>
180
90
  <form action="./update.php" method="post" name="form">
181
91
  <input type="hidden" name="id" value="<?php echo $dbCulum[0]['id']; ?>">
182
92
  <table class="contact-table">
@@ -220,8 +130,104 @@
220
130
 
221
131
  <div>
222
132
  </body>
133
+
223
- ```
134
+ ```
135
+
224
-
136
+ ```Db.php
137
+ <?php
138
+ require_once(ROOT_PATH .'./database.php');
139
+
140
+ class RegisterDb
141
+ {
142
+ public function submitDb()
143
+ {
144
+ $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8';
145
+ $user = 'root';
146
+ $password = 'root';
147
+ try {
148
+ $dbh = new PDO($dsn, $user, $password);
149
+ $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
150
+ $dbh->beginTransaction();
151
+
152
+ // $fullname = $_SESSION['fullname'];
153
+ // $furigana = $_SESSION['furigana'];
154
+ // $tellNumber = $_SESSION['tellNumber'];
155
+ // $email = $_SESSION['email'];
156
+ // $message = $_SESSION['message'];
157
+
158
+ $fullname = $_POST['fullname'];
159
+ $kana = $_POST['furigana'];
160
+ $tel = $_POST['tellNumber'];
161
+ $email = $_POST['email'];
162
+ $body = $_POST['message'];
163
+
164
+ $sql = "INSERT INTO contacts (fullname, kana, tel, email, body) VALUES (:fullname, :furigana, :tellNumber, :email, :message)";
165
+ $stmt = $dbh->prepare($sql);
166
+ $params = array(':fullname' => $fullname, 'furigana' => $kana, 'tellNumber' => $tel, 'email' => $email, 'message' => $body);
167
+ $stmt->execute($params);
168
+ $dbh->commit();
169
+ } catch (PDOException $e) {
170
+ echo "接続失敗: " . $e->getMessage() . "\n";
171
+ exit();
172
+ }
173
+ }
174
+ }
175
+
176
+ class DbData
177
+ {
178
+ public $contactsData;
179
+
180
+ public function getData()
181
+ {
182
+ $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8';
183
+ $user = 'root';
184
+ $password = 'root';
185
+ try {
186
+ $dbh = new PDO($dsn, $user, $password);
187
+ $stmt = $dbh->prepare('SELECT * FROM contacts');
188
+ $stmt->execute();
189
+ $contactsData = $stmt->fetchAll(PDO::FETCH_ASSOC);
190
+ echo '接続完了';
191
+ $this->contactsData = $contactsData;
192
+ return $contactsData;
193
+ } catch (PDOException $e) {
194
+ echo "接続失敗: " . $e->getMessage() . "\n";
195
+ exit();
196
+ } finally {
197
+ $dbh = null;
198
+ }
199
+ }
200
+ }
201
+
202
+ class UpdateData
203
+ {
204
+ public $dbCulum;
205
+
206
+ public function dbUpdate()
207
+ {
208
+ $dsn = 'mysql:host=localhost;dbname=casteria;charset=utf8';
209
+ $user = 'root';
210
+ $password = 'root';
211
+ $id = $_GET['id'];
212
+ try {
213
+ $dbh = new PDO($dsn, $user, $password);
214
+ $stmt = $dbh->prepare('SELECT * FROM contacts');
215
+ $stmt->bindParam(":id", $id);
216
+ $stmt->execute();
217
+ $dbCulum = $stmt->fetchAll(PDO::FETCH_ASSOC);
218
+
219
+ echo '接続完了';
220
+ $this->dbCulum = $dbCulum;
221
+ return $dbCulum;
222
+ } catch (PDOException $e) {
223
+ echo "接続失敗: " . $e->getMessage() . "\n";
224
+ exit();
225
+ } finally {
226
+ $dbh = null;
227
+ }
228
+ }
229
+ }
230
+ ```
225
231
 
226
232
  ### 試したこと
227
233
  下記のようにupdate.phpに入力されているinputタグのvalue値を変更してみたが解決しませんでした。