質問編集履歴

3

タグ追加

2022/03/22 06:03

投稿

masaa0802
masaa0802

スコア28

test CHANGED
File without changes
test CHANGED
File without changes

2

まずデータベースにINSERTされるようにしたいので、部分的に書き直しました。かつデータベースへの接続ファイルが複数存在したので、一つにまとめました。

2022/03/22 05:22

投稿

masaa0802
masaa0802

スコア28

test CHANGED
@@ -1 +1 @@
1
- MVCでPOSTしたデータが反映されない
1
+ PHP(MVCINSETされない
test CHANGED
@@ -12,134 +12,99 @@
12
12
  ```php
13
13
  <!-- contact.php -->
14
14
 
15
- <?php
16
- <h1> 入力画面</h1>
17
- <p>* は必須項目です</p>
18
- <?php if(!empty($errors) ): ?>
19
- <ul class="error_list">
20
- <?php foreach( $errors as $value ): ?>
21
- <li><?php echo $value; ?></li>
22
- <?php endforeach; ?>
23
- </ul>
24
- <?php endif; ?>
25
- <form action= "" method="post">
26
- <div class="row">
27
- <!-- 氏名 -->
28
- <div class="element_wrap">
29
- <label for="name">*氏名</label>
30
- <input type="text" name="name" id="name" placeholder="例)入力太郎"
31
- value="<?php if( !empty($_POST['name']) ){ echo $_POST['name']; } ?>"><br>
32
- </div>
33
- <!-- フリガナ -->
34
- <div class="element_wrap">
35
- <label for="kana">*フリガナ</label>
36
- <input type="text" name="kana" id="kana" placeholder="例)ニュウリョクタロウ"
37
- value="<?php if( !empty($_POST['kana']) ){ echo $_POST['kana']; } ?>"><br>
38
- </div>
39
- <!-- 電話番号 -->
40
- <div class="element_wrap">
41
- <label for="tel">電話番号</label>
42
- <input type="text" name="tel" id="tel" placeholder="例)08011112222"
43
- value="<?php if( !empty($_POST['tel']) ){ echo $_POST['tel']; } ?>"><br>
44
- </div>
45
- <!-- メーアドレス -->
46
- <div class="element_wrap">
47
- <label for="email">*メールアドレス</label>
48
- <input type="text" name="email" id="email" placeholder="例)nyuuryoku11@mail.com"
49
- value="<?php if( !empty($_POST['email']) ){ echo $_POST['email']; } ?>"><br>
50
- </div>
51
- <!-- お問い合わせ内容 -->
52
- <div class="element_wrap">
53
- <label for="body">*お問い合わせ内容</label><br>
54
- <textarea rows="10" name="body" id="body" ><?php if (!empty($_POST['body'])){ echo nl2br($_POST['body']); } ?></textarea><br>
55
- </div>
56
- <input name = "btn_confirm" type="submit" value="入力内容を確認する">
57
- </div>
58
- </form>
59
- <!-- -------------------------------------(入力画面)お問い合わせ内容詳細------------------------------------- -->
60
- <form action="" method="post">
61
- <table>
62
- <th>氏名</th>
63
- <th>フリガナ</th>
64
- <th>電話番号</th>
65
- <th>メールアドレス</th>
66
- <th>お問い合わせ内容</th>
67
- <th></th>
68
- <th></th>
69
- <?php if( !empty($message_array) ){ ?>
70
- <?php foreach( $message_array as $value ){ ?>
71
- <tr>
72
- <td><p><?php echo $value['name']; ?></p></td>
73
- <td><p><?php echo $value['kana']; ?></p></td>
74
- <td><p><?php echo $value['tel']; ?></p></td>
75
- <td><p><?php echo $value['email']; ?></p></td>
76
- <td><p><?php echo nl2br($value['body']); ?></p></td>
77
- <td><a href="edit.php?message_id=<?php echo $value['id']; ?>" name="edit">編集</a></td>
78
- <td><a href="delete.php?message_id=<?php echo $value['id']; ?>" onclick="return confirm('本当に削除しますか?')" name="delete">削除</a></td>
79
- </tr>
80
- <?php } ?>
81
- <?php } ?>
82
- </table>
15
+ <!-- 省略 -->
16
+
17
+ <?php if( $page_flag === 1 ): ?>
18
+ <h1> 確認画面</h1>
19
+ <form action= "" method="post">
20
+ <!-- 氏名 -->
21
+ <div class="element_wrap_p">
22
+ <label for="name">氏名</label>
23
+ <p><?php echo $_POST['name']; ?></p>
24
+ </div>
25
+ <!-- フリガナ -->
26
+ <div class="element_wrap_p">
27
+ <label for="kana">フリガナ</label>
28
+ <p><?php echo $_POST['kana']; ?></p>
29
+ </div>
30
+ <!-- 電話番号 -->
31
+ <div class="element_wrap_p">
32
+ <label for="tel">電話番号</label>
33
+ <p><?php echo $_POST['tel']; ?></p>
34
+ </div>
35
+ <!-- メールアドレス -->
36
+ <div class="element_wrap_p">
37
+ <label for="email">メールアドレス</label>
38
+ <p><?php echo $_POST['email']; ?></p>
39
+ </div>
40
+ <!-- お問い合わせ内容 -->
41
+ <div class="element_wrap_p">
42
+ <label for="body">*お問い合わせ内容</label><br>
43
+ <p><?php echo nl2br($_POST['body']); ?></p>
44
+ </div>
45
+ <input type="submit" name="btn_back" value="キャンセ">
46
+ <input type="submit" name="btn_submit" value="送信">
47
+ <input type="hidden" name="name" value="<?php echo $_POST['name']; ?>">
48
+ <input type="hidden" name="kana" value="<?php echo $_POST['kana']; ?>">
49
+ <input type="hidden" name="tel" value="<?php echo $_POST['tel']; ?>">
50
+ <input type="hidden" name="email" value="<?php echo $_POST['email']; ?>">
51
+ <input type="hidden" name="body" value="<?php echo $_POST['body']; ?>">
83
52
  </form>
84
53
 
85
54
  ```
86
55
 
87
56
  ```php
88
57
  <!-- ContactController.php -->
58
+
89
- <?php
59
+ // <?php
90
60
  require_once(ROOT_PATH .'Models/ContactModel.php');
91
61
 
92
62
  class ContactController {
93
- private $request; // リクエストパラメータ(GET,POST)
63
+ private $request; // リクエストパラメータ(GET,POST)
94
- private $Contact; // Contactモデル
64
+ private $Contact; // Contactモデル
95
65
 
96
- public function __construct() {
66
+ public function __construct() {
97
- // リクエストパラメータの取得
67
+ // リクエストパラメータの取得
98
- $this->request['get'] = $_GET;
68
+ $this->request['get'] = $_GET;
99
- $this->request['post'] = $_POST;
69
+ $this->request['post'] = $_POST;
100
- // モデルオブジェクトの生成
101
- $this->Contact = new Contact();
102
- // 別モデルと連携
103
- $dbh = $this->Contact->get_db_handler();
104
- }
105
70
 
71
+ // モデルオブジェクトの生成
106
- public function index() {
72
+ $this->Contact = new Contact();
73
+ // 別モデルと連携
107
- $contacts= $this->Contact->contactAll();
74
+ $dbh = $this->Contact->get_db_handler();
108
- $params = [
109
- 'contacts' => $contacts,
110
- ];
111
- return $params;
112
- }
75
+ }
113
76
 
114
- public function index1() {
77
+ public function insertController() {
78
+
79
+ if(isset($this->request['post']['name'])) {
80
+ $name = $this->request['post']['name'];
81
+ }
82
+ if(isset($this->request['post']['kana'])) {
83
+ $kana = $this->request['post']['kana'];
84
+ }
85
+ if(isset($this->request['post']['tel'])) {
86
+ $tel = $this->request['post']['tel'];
87
+ }
88
+ if(isset($this->request['post']['email'])) {
89
+ $email = $this->request['post']['email'];
90
+ }
91
+ if(isset($this->request['post']['body'])) {
92
+ $body = $this->request['post']['body'];
93
+ }
115
- $contacts= $this->Contact->insert();
94
+ $contacts = $this->Contacts->findAll();
116
- $insert = $this->Contact->insert();
95
+ $insert = $this->Contacts->insert();
117
- $params1 = [
96
+ $params = [
118
- 'contacts' => $contacts,
97
+ 'contacts' => $contacts,
119
- 'insert' => $insert,
98
+ 'insert' => $insert,
99
+ 'name' => $name,
100
+ 'kana' => $kana,
101
+ 'tel' => $tel,
102
+ 'email' => $email,
103
+ 'body' => $body,
120
- ];
104
+ ];
121
- return $params1;
105
+ return $params;
122
- }
106
+ }
123
-
124
- public function index2() {
125
- $contacts = $this->Contact->contactAll();
126
- $delete = $this->Contact->delete();
127
- $params2 = [
128
- 'contacts' => $contacts,
129
- 'delete' => $delete,
130
- ];
131
- return $params2;
132
- }
107
+ }
133
-
134
- public function index3() {
135
- $contacts = $this->Contact->contactAll();
136
- $edit = $this->Contact->edit();
137
- $params3 = [
138
- 'contacts' => $contacts,
139
- 'edit' => $edit,
140
- ];
141
- return $params3;
142
- }
143
108
 
144
109
  }
145
110
 
@@ -147,138 +112,79 @@
147
112
  ```php
148
113
 
149
114
  <!-- ContactModel.php -->
115
+
116
+ // <?php
150
- <?php
117
+ <?php
151
- require_once(ROOT_PATH .'Models/Db.php');
118
+ require_once(ROOT_PATH .'datebase.php');
152
119
 
153
120
  class Contact extends Db {
154
121
  public function __construct($dbh = null) {
155
122
  parent::__construct($dbh);
156
123
  }
157
124
  /**
158
- * contactsテーブルからすべてデータを取得
125
+ * contactsテーブルからすべてデータを取得(20件ごと)
159
126
  */
160
- public function contactAll() {
127
+ public function findAll(){
161
- $sql = "SELECT * FROM contacts WHERE id = :id";
128
+ $sql = "SELECT * FROM contacts WHERE id = :id";
162
- $sth = $this->dbh->prepare($sql);
129
+ $sth = $this->dbh->prepare($sql);
163
- $sth->execute();
130
+ $sth->execute();
164
- $contactAll = $sth->fetchAll(PDO::FETCH_ASSOC);
165
- return $contactAll;
166
- }
167
- /**
168
- * contactsテーブルから各データを挿入
169
- */
170
- public function insert() {
171
- $sql = "INSERT INTO contacts (name, kana, tel, email, body) VALUES (:name, :kana, :tel, :email, :body)";
172
- $sth = $this->dbh->prepare($sql);
173
- $sth->execute();
174
- $insert = $sth->fetchAll(PDO::FETCH_ASSOC);
131
+ $result = $sth->fetchAll(PDO::FETCH_ASSOC);
175
- return $insert;
132
+ return $result;
176
133
  }
177
134
 
178
- /**
179
- * contactsテーブルから各データを更新
180
- */
181
- public function edit() {
135
+ public function insert() {
136
+ try {
182
- $sql = "UPDATE contacts SET name = :name, kana = :kana, tel = :tel, email = :email , body = :body WHERE id = :id";
137
+ $sql = 'INSERT INTO contacts (name, kana, tel, email, body) VALUES (:name, :kana, :tel, :email, :body)';
183
- $sth = $this->dbh->prepare($sql);
138
+ $sth = $this->dbh->prepare($sql);
184
- $sth->execute();
139
+ $sth->execute();
185
- $update = $sth->fetchColumn();
140
+ $result = $sth->fetchColumn();
186
- return $update;
141
+ return $result;
142
+ }catch(Exception $e) {
143
+ // エラーが発生した時はロールバック
144
+ $sth->rollBack();
145
+ echo $e;
146
+ }
187
147
  }
188
- /**
189
- * contactsテーブルから各データを削除
190
- */
191
- public function delete() {
192
- $sql = "DELETE FROM contacts WHERE id = :id";
193
- $sth = $this->dbh->prepare($sql);
194
- $sth->execute();
195
- $delete = $sth->fetchColumn();
196
- if( $delete ) {
197
- header("Location: contact.php");
198
- exit;
199
- }
200
- return $delete;
201
- }
202
-
203
148
  }
204
- ```
205
- ```php
206
- <!-- Db.php -->
207
- <?php
208
- require_once(ROOT_PATH .'database.php');
209
- // 使用する変数を初期化
210
- $name = null;
211
- $kana = null;
212
- $tel= null;
213
- $email = null;
214
- $body = null;
215
-
216
- $message_array = array();
217
- $message_date = null;
218
- $pdo = null;
219
- $stmt = null;
220
- $res = null;
221
-
222
-
223
- class Db {
224
- protected $dbh;
225
-
226
- public function __construct($dbh = null) {
227
- if(!$dbh) { // 接続情報が存在しない場合
228
- try {
229
- $option = null;
230
- $this->dbh = new PDO('mysql:charset=UTF8;dbname=casteria;host=127.0.0.1;port=8111', 'root', '', $option);
231
- // 接続成功
232
- } catch (PDOException $e) {
233
- echo "接続失敗: " . $e->getMessage() . "\n";
234
- exit();
235
- }
236
- } else { // 接続情報が存在する場合
237
- $this->dbh = $dbh;
238
- }
239
- }
240
-
241
- public function get_db_handler() {
242
- return $this->dbh;
243
- }
244
-
245
- public function begin_transaction() {
246
- $this->dbh->beginTransaction();
247
- }
248
-
249
- public function commit() {
250
- $this->dbh->commit();
251
- }
252
-
253
- public function rollback() {
254
- $this->dbh->rollback();
255
- }
256
- }
257
-
258
-
259
-
260
149
  ```
261
150
  ```php
262
151
  <!-- datebase.php -->
263
152
 
264
- <?php
153
+ <?php
154
+ class Db {
155
+ protected $dbh;
265
156
 
157
+ public function __construct($dbh = null) {
266
- // データベースに接続
158
+ if(!$dbh) { // 接続情報が存在しない場合
267
- try {
159
+ try {
160
+ $this->dbh = new PDO('mysql:charset=UTF8;dbname=casteria;host=127.0.0.1;port=8111', 'root', '');
268
161
 
162
+ // 接続成功
163
+ } catch (PDOException $e) {
164
+ echo "接続失敗: " . $e->getMessage() . "\n";
165
+ exit();
166
+ }
167
+ } else { // 接続情報が存在する場合
269
- $option = array(
168
+ $this->dbh = $dbh;
270
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
271
- PDO::MYSQL_ATTR_MULTI_STATEMENTS => false
272
- );
169
+ }
273
- $pdo = new PDO('mysql:charset=UTF8;dbname=casteria;host=127.0.0.1;port=8111', 'root', '', $option);
170
+ }
274
171
 
275
- } catch(PDOException $e) {
172
+ public function get_db_handler() {
173
+ return $this->dbh;
174
+ }
276
175
 
176
+ public function begin_transaction() {
177
+ $this->dbh->beginTransaction();
178
+ }
179
+
180
+ public function commit() {
277
- // 接続エラーのときエラー内容を取得する
181
+ $this->dbh->commit();
182
+ }
183
+
184
+ public function rollback() {
278
- $error_message[] = $e->getMessage();
185
+ $this->dbh->rollback();
186
+ }
279
187
  }
280
- // データベースの接続を閉じる
281
- $pdo = null;
282
188
 
283
189
  ```
284
190
 

1

実現したいことの内容をより詳細に記載しました。

2022/03/18 11:47

投稿

masaa0802
masaa0802

スコア28

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,10 @@
2
2
 
3
3
  現在PHPのCRUD機能を実装したお問い合わせフォームを作成しております。
4
4
  MVCの形式で機能実装している際にPOSTしたデータがデータベースに反映されなくなってしまいました。
5
+ contact.php(view)は入力画面の一部なのですが、入力画面⇒確認画面⇒完了画面のデータが受け渡されているのは確認が取れています。しかし、確認画面の送信ボタンを押した後、controller→modelと処理されておらず、ただ画面が切り替わっているだけという状況です。
5
- 間違っ箇所をご教示いただけます幸いです。
6
+ 実現したいことはPOSTし後controllermodelの処理が実行され、データベースにデータが反映されることです。
7
+
8
+
6
9
 
7
10
  ### ソースコード
8
11