teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

テーブルに追加

2021/02/25 13:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,10 @@
1
1
  PHPで詳細表示をしたく現在実装しています。
2
2
 
3
+ テーブルは以下のように記載しています。
4
+ 以下はusersテーブル
5
+ ![イメージ説明](ba365b77cced8c809eca6a549aabb7b9.png)
6
+ 以下はtweetsテーブル
7
+ ![イメージ説明](83d4cfae722a304f016e702f31832d17.png)
3
8
  詰まっている部分としては詳細表示の際に、ログインしているユーザーでかつそれを投稿したユーザーである場合は、編集と削除ができる。
4
9
 
5
10
  テーブルはusersテーブル、tweetsテーブルを使用しています。

4

修正しました。

2021/02/25 13:11

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -103,4 +103,235 @@
103
103
  <body>
104
104
  <script src='https://code.jquery.com/jquery-3.4.1.min.js'></script>
105
105
  </body>
106
+ ```
107
+
108
+ ```
109
+ //ユーザー登録用register.php
110
+
111
+ <?php
112
+ session_start();
113
+ require('../common/database.php');
114
+ require('../common/validate.php');
115
+
116
+ require('../common/head_info.php');
117
+ ?>
118
+
119
+ <?php
120
+ //POST送信された場合
121
+ if(!empty($_POST)) {
122
+ $name = $_POST['name'];
123
+ $email = $_POST['email'];
124
+ $pass = $_POST['pass'];
125
+ $pass_re = $_POST['pass_re'];
126
+
127
+
128
+ //未入力チェック
129
+ validateNot($name, 'name');
130
+ validateNot($email, 'email');
131
+ validateNot($pass, 'pass');
132
+ validateNot($pass_re, 'pass_re');
133
+
134
+ if(empty($err_msg)) {
135
+ //ニックネームの最大文字数チェック
136
+ validateNameMaxLen($name, 'name');
137
+
138
+ //メールアドレスの重複チェック
139
+
140
+ //メールアドレスの形式チェック
141
+
142
+ //パスワードの最大文字数チェック
143
+ validatePassMaxLen($pass,'pass');
144
+ //パスワードの最小文字数チェック
145
+ validatePassMinLen($pass,'pass');
146
+ //パスワードの半角英数字チェック
147
+
148
+
149
+ if(empty($err_msg)) {
150
+ try {
151
+ //DB接続処理
152
+ $database_handler = getDatabaseConnection();
153
+ // プリペアドステートメントで SQLをあらかじめ用意しておく
154
+ if($statement = $database_handler->prepare('INSERT INTO users (name, email, password) VALUES (:name, :email, :password)')){
155
+ $password = password_hash($pass, PASSWORD_DEFAULT);
156
+ //指定された変数名にパラメータをバインド(紐付け)
157
+ $statement->bindParam(':name', $name);
158
+ $statement->bindParam(':email', $email);
159
+ $statement->bindParam(':password', $password);
160
+ $statement->execute();
161
+ }
162
+
163
+ // ユーザー情報保持
164
+ $_SESSION['user'] = [
165
+ 'id' => $database_handler->lastInsertId()
166
+ ];
167
+ }catch(Exception $e) {
168
+ error_log('エラー発生:' . $e -> getMessage());
169
+ $err_msg['common'] = message05;
170
+ exit;
171
+ }
172
+ header('Location:../tweets/index.php');
173
+ exit;
174
+ }
175
+ }
176
+ }
177
+ ?>
178
+
179
+ <?php
180
+ require('../common/header.php');
181
+ ?>
182
+
183
+ <div class='main-top'>
184
+ <div class='form-register'>
185
+ <div class="form-register-list">
186
+ <h2><i class="fas fa-user-plus"></i>ユーザー登録</h2>
187
+ <form action="" method='post' class='form'>
188
+ <label>
189
+ <input type="text" name='name' placeholder="ニックネーム" value="<?php print(htmlspecialchars($_POST['name'],ENT_QUOTES));?>">
190
+ <div class="error_mes">
191
+ <?php
192
+ if(!empty($err_msg['name'])) echo $err_msg['name'];
193
+ ?>
194
+ </div>
195
+ </label>
196
+ <label>
197
+ <input type="text" name='email' placeholder="メールアドレス" value="<?php print(htmlspecialchars($_POST['email'],ENT_QUOTES));?>">
198
+ <div class="error_mes">
199
+ <?php
200
+ if(!empty($err_msg['email'])) echo $err_msg['email'];
201
+ ?>
202
+ </div>
203
+ </label>
204
+ <label>
205
+ <input type="password" name='pass' placeholder="パスワード" value="<?php print(htmlspecialchars($_POST['pass'],ENT_QUOTES));?>"></br>
206
+ <div class="error_mes">
207
+ <?php
208
+ if(!empty($err_msg['pass'])) echo $err_msg['pass'];
209
+ ?>
210
+ </div>
211
+ <span class='form-rule'>※英数字8文字以上</span>
212
+ </label>
213
+ <label>
214
+ <input type="password" name='pass_re' placeholder="パスワード確認" value="<?php print(htmlspecialchars($_POST['pass_re'],ENT_QUOTES));?>"></br>
215
+ <div class="error_mes">
216
+ <?php
217
+ if(!empty($err_msg['pass_re'])) echo $err_msg['pass_re'];
218
+ ?>
219
+ </div>
220
+ <span class='form-rule'>※英数字8文字以上</span>
221
+ </label>
222
+ <div class='button-container'>
223
+ <input type="submit" value='登録する'>
224
+ </div>
225
+ </form>
226
+ </div>
227
+ </div>
228
+ </div>
229
+
230
+ <?php
231
+ require('../common/footer.php');
232
+ ?>
233
+ ```
234
+
235
+ ```
236
+ //tweetの一覧
237
+ <?php
238
+ session_start();
239
+ require('../common/validate.php');
240
+ require('../common/database.php');
241
+ require('../common/head_info.php');
242
+ $database_handler = getDatabaseConnection();
243
+ $tweets=$database_handler->prepare('SELECT * FROM tweets ORDER BY id DESC;');
244
+ $tweets->execute();
245
+ ?>
246
+ <?php
247
+ require('../common/header.php');
248
+ ?>
249
+
250
+ <div class="main-top">
251
+ <?php
252
+ if(!empty($_SESSION['user']['id'])) {
253
+ ?>
254
+ <div class="community-create">
255
+ <div class="tweet-top">
256
+ <a href="../tweets/create.php">投稿する</a>
257
+ </div>
258
+ </div>
259
+ <?php
260
+ }
261
+ ?>
262
+ <div class="create-list">
263
+ <div class="create-list-container">
264
+ <div class="create-list-top">
265
+ <p>
266
+ aaa
267
+ </p>
268
+ </div>
269
+ <?php
270
+ if(!empty($tweets)){
271
+ ?>
272
+ <ul class="create-list-content">
273
+ <?php foreach($tweets as $tweet): ?>
274
+ <li class="list-menu">
275
+ <div class="title-list">
276
+ <p><a href="community01_show.php?id=<?php print($tweet['id']);?>"><?php print(mb_substr($tweet['title'],0,50)); ?></a></p>
277
+ </div>
278
+ </li>
279
+ <?php endforeach; ?>
280
+ </ul>
281
+ <?php
282
+ }else{
283
+ ?>
284
+ <p style="text-align:center;line-height:20;">投稿はまだありません</p>
285
+ <?php
286
+ }
287
+ ?>
288
+
289
+ </div>
290
+ </div>
291
+ </div>
292
+
293
+
294
+ <?php
295
+ require('../common/footer.php');
296
+ ?>
297
+ ```
298
+
299
+ ```
300
+ //tweetのshow.php
301
+
302
+ <?php
303
+ session_start();
304
+ require('../common/auth.php');
305
+ require('../common/validate.php');
306
+ require('../common/database.php');
307
+ require('../common/head_info.php');
308
+ $user_id = getLoginUserId();
309
+ $database_handler = getDatabaseConnection();
310
+ $tweets=$database_handler->prepare('SELECT * FROM tweets WHERE id=?;');
311
+ $tweets->execute();
312
+ $tweet = $tweets->fetch();
313
+ ?>
314
+ <?php
315
+ require('../common/header.php');
316
+ ?>
317
+
318
+ <div class="main-top">
319
+ <div class="community-show">
320
+ <article>
321
+ <h1 class="item-name"><?php print($tweet['title']);?></h1>
322
+
323
+ <div class="item-main-content"><?php print($tweet['content']);?></div>
324
+ <?php
325
+ if($tweet['user_id']==$user_id) {
326
+ ?>
327
+ <a href="community01_edit.php?id=<?php print($tweet['id']);?>">編集する</a>
328
+ <a href="community01_delete.php?id=<?php print($tweet['id']);?>">削除する</a>
329
+ <button type="submit" class="btn btn-danger" formaction="./tweets/delete.php"><i class="fas fa-trash-alt"></i></button>
330
+ <?php
331
+ }
332
+ ?>
333
+ </article>
334
+ </div>
335
+ </div>
336
+
106
337
  ```

3

記載変更

2021/02/25 13:09

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  詰まっている部分としては詳細表示の際に、ログインしているユーザーでかつそれを投稿したユーザーである場合は、編集と削除ができる。
4
4
 
5
+ テーブルはusersテーブル、tweetsテーブルを使用しています。
6
+ ここで、usersテーブルのログインしているユーザーとtweetsテーブルの投稿した記事を紐付けるSQL文を書いて行きたいと思っています。
7
+
5
8
  そして、詳細の際に、選択した特定の一点のみを取得するSQL文に詰まっています。
6
9
 
7
10
  SQL分で`SELECT * FROM tweets Where id=?;`のように記載することも可能だと思うのですが、その深い部分が理解できないため、?は使わずにgetLoginUserIdという関数を使っています。

2

SQL変更

2021/02/25 12:00

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  そして、詳細の際に、選択した特定の一点のみを取得するSQL文に詰まっています。
6
6
 
7
- SQL分で`SELECT * FROM tweets id=?;`のように記載することも可能だと思うのですが、その深い部分が理解できないため、?は使わずにgetLoginUserIdという関数を使っています。
7
+ SQL分で`SELECT * FROM tweets Where id=?;`のように記載することも可能だと思うのですが、その深い部分が理解できないため、?は使わずにgetLoginUserIdという関数を使っています。
8
8
  また、`$_SESSION['user']['id'])==$user_id`と記載してログインしているユーザーが投稿したユーザーと同一であると記載したかったのですが、フレームワークではなく生のPHPで書くとなるとそのような記事がなかなか見当たらず詰まってしまいます。
9
9
  原因、実現したいこととして解決策のご提示をお願いします。
10
10
 
@@ -19,7 +19,7 @@
19
19
  require('../common/head_info.php');
20
20
  $user_id = getLoginUserId();
21
21
  $database_handler = getDatabaseConnection();
22
- $tweets=$database_handler->prepare('SELECT * FROM tweets ORDER BY id DESC;');
22
+ $tweets=$database_handler->prepare('SELECT * FROM tweets where id=user_id;');
23
23
  $tweets->execute();
24
24
  $tweet = $tweets->fetch();
25
25
  ?>

1

記載変更

2021/02/25 11:44

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -15,7 +15,6 @@
15
15
  <?php
16
16
  session_start();
17
17
  require('../common/auth.php');
18
- require('../common/validate.php');
19
18
  require('../common/database.php');
20
19
  require('../common/head_info.php');
21
20
  $user_id = getLoginUserId();
@@ -101,8 +100,4 @@
101
100
  <body>
102
101
  <script src='https://code.jquery.com/jquery-3.4.1.min.js'></script>
103
102
  </body>
104
- ```
105
-
106
- ```
107
-
108
103
  ```