質問編集履歴

1

PHP,MySQLタグの追加、コードの追加

2019/07/08 08:59

投稿

toki38
toki38

スコア14

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 問題点
2
2
 
3
- 入力画面でtextareaに入力したテキストを改行すると、表示画面で改行部分が\r\nとでてきてしまう。
3
+ 入力画面(post.php)でtextareaに入力したテキストを改行すると、表示画面(home.php)で改行部分が\r\nとでてきてしまう。
4
4
 
5
5
 
6
6
 
@@ -64,4 +64,314 @@
64
64
 
65
65
 
66
66
 
67
+ ### コード
68
+
69
+ ```
70
+
71
+ (post.php)
72
+
73
+ <?php
74
+
75
+ session_start();
76
+
77
+  //DB接続
78
+
79
+ include_once('dbconnect.php');
80
+
81
+
82
+
83
+ if (!isset($_SESSION['user'])) {
84
+
85
+ header('location:login.php');
86
+
87
+ }
88
+
89
+
90
+
91
+ //投稿するボタンを押したときの処理
92
+
93
+ if (isset($_POST['posting'])) {
94
+
95
+ $post_name = $mysqli->real_escape_string($_POST['post_name']);
96
+
97
+ $post_author = $mysqli->real_escape_string($_POST['post_author']);
98
+
99
+ $post_introduction = $mysqli->real_escape_string($_POST['post_introduction']);
100
+
101
+ $post_image = "bookimage/".$_FILES['post_image']['name'];
102
+
103
+ //uses_idをpost_idに代入
104
+
105
+ $query2 = "SELECT user_id FROM users WHERE user_id=".$_SESSION['user']."";
106
+
107
+ $result = $mysqli->query($query2);
108
+
109
+ while ($row = $result->fetch_assoc()) {
110
+
111
+ $post_id = $row['user_id'];
112
+
113
+ }
114
+
115
+ //POSTされた情報をDBへ格納する
116
+
117
+ $query = "INSERT INTO posts SET post_name=?, post_author=?, post_introduction=?, post_image=?, post_id=?";
118
+
119
+ $stmt = $mysqli->prepare($query);
120
+
121
+ $stmt->bind_param("ssssi", $post_name, $post_author, $post_introduction, $post_image, $post_id);
122
+
123
+ $stmt->execute();
124
+
125
+ move_uploaded_file($_FILES['post_image']['tmp_name'], $post_image);
126
+
127
+
128
+
129
+ }
130
+
131
+
132
+
133
+ ?>
134
+
135
+
136
+
137
+ <!doctype html>
138
+
139
+ <html lang="ja">
140
+
141
+ <head>
142
+
143
+ <meta charset="utf-8">
144
+
145
+ 〜省略(Bootstrap使ってます)〜
146
+
147
+ </head>
148
+
149
+ <body>
150
+
151
+
152
+
153
+ <!-- Posting-wrapper -->
154
+
155
+ <div class="posting-wrapper py-5">
156
+
157
+ <div class="container">
158
+
159
+ <div class="row">
160
+
161
+ <div class="col-md-6 offset-md-3 text-center">
162
+
163
+ <h2 class="mb-5 p_title">投稿ページ</h2>
164
+
165
+ <form method="post" enctype="multipart/form-data">
166
+
167
+ <label class="col-form-label">タイトル <span class="bg-danger text-white p-1" >必須</span></label>
168
+
169
+ <input type="text" name="post_name" class="form-control" required/>
170
+
171
+ <label class="col-form-label">作者 <span class="bg-danger text-white p-1" >必須</span></label>
172
+
173
+ <input type="text" name="post_author" class="form-control" required/>
174
+
175
+ <label class="col-form-label">画像 <span class="bg-danger text-white p-1" >必須</span></label>
176
+
177
+ <input type="file" name="post_image" class="form-control" required/>
178
+
179
+ <label for="exampleTextarea">紹介文</label>
180
+
181
+ <!-- 質問の部分 -->
182
+
183
+ <textarea name="post_introduction" class="form-control" rows="8" cols="80"></textarea>
184
+
185
+          <!-- 質問ここまで -->
186
+
187
+ <div class="text-center">
188
+
189
+ <input type="submit" name="posting" class="btn btn-lg btn-success mt-4" value="投稿する">
190
+
191
+ </div>
192
+
193
+ </form>
194
+
195
+ </div>
196
+
197
+ </div>
198
+
199
+ </div>
200
+
201
+ </div>
202
+
203
+ <!-- End Posting-wrapper -->
204
+
205
+
206
+
207
+ </body>
208
+
209
+ </html>
210
+
211
+
212
+
213
+ ```
214
+
215
+
216
+
217
+ ```
218
+
219
+ (home.php)
220
+
221
+ <?php
222
+
223
+ session_start();
224
+
225
+  //DB接続
226
+
227
+ include_once('dbconnect.php');
228
+
229
+
230
+
231
+ if (!isset($_SESSION['user'])) {
232
+
233
+ header('location:login.php');
234
+
235
+ }
236
+
237
+
238
+
239
+  //usersテーブルとpostsテーブルをリレーションさせる
240
+
241
+ $query = "SELECT * FROM posts LEFT JOIN users ON posts.post_id = users.user_id ORDER BY id DESC";
242
+
243
+ $result = $mysqli->query($query);
244
+
245
+ // 結果を受け取る変数を配列にする
246
+
247
+ $posteds = [];
248
+
249
+ while ($row = $result->fetch_assoc()) {
250
+
251
+ // 配列に取り出した一行分の結果を足していく
252
+
253
+ $posteds[] = $row;
254
+
255
+ }
256
+
257
+
258
+
259
+ ?>
260
+
261
+
262
+
263
+ <!doctype html>
264
+
265
+ <html lang="ja">
266
+
267
+ <head>
268
+
269
+ <meta charset="utf-8">
270
+
271
+ 〜省略(Bootstrap使ってます)〜
272
+
273
+ </head>
274
+
275
+ <body>
276
+
277
+
278
+
279
+ <!-- top-wrapper -->
280
+
281
+ <div class="top-wrapper text-center text-white">
282
+
283
+ <a href="post.php" class="btn btn-lg btn-success mt-3"><span><i class="fas fa-book-open"></i></span> 投稿ページへ</a>
284
+
285
+ </div>
286
+
287
+ <!-- End top-wrapper -->
288
+
289
+
290
+
291
+ <!-- post-wrapper -->
292
+
293
+ <div class="post-wrapper pt-5">
294
+
295
+ <div class="container">
296
+
297
+ <div class="row">
298
+
299
+ <!-- 投稿を繰り返し表示 -->
300
+
301
+ <?php foreach ($posteds as $posted): ?>
302
+
303
+ <div class="col-md-4 text-center mb-3">
304
+
305
+ <img src="<?php echo $posted['post_image']; ?>" width="200px" height="300px">
306
+
307
+ </div>
308
+
309
+ <div class="col-md-8 mb-3 manga_data">
310
+
311
+ <h3><?php echo $posted['post_name']; ?></h3>
312
+
313
+ <p><?php echo $posted['post_author']; ?></p>
314
+
315
+        <!-- 質問の部分 -->
316
+
317
+ <p><?php echo nl2br(htmlspecialchars($posted['post_introduction'])); ?></p>
318
+
319
+        <!-- 質問ここまで -->
320
+
321
+ </div>
322
+
323
+ <div class="col-md-4 text-center mb-3">
324
+
325
+ <img src="<?php echo $posted['photo']; ?>" width="120px" height="120px" class="rounded-circle">
326
+
327
+ <p>紹介者 : <?php echo $posted['username']; ?>さん</p>
328
+
329
+ </div>
330
+
331
+ <div class="col-12">
332
+
333
+ <hr style="background-color:#EB6964" class="mb-4" >
334
+
335
+ </div>
336
+
337
+ <?php endforeach; ?>
338
+
339
+ <!-- ここまで繰り返し -->
340
+
341
+ </div>
342
+
343
+ </div>
344
+
345
+ </div>
346
+
347
+ <!-- End post-wrapper -->
348
+
349
+
350
+
351
+ </body>
352
+
353
+ </html>
354
+
355
+
356
+
357
+
358
+
359
+ ```
360
+
67
361
  ご助言頂けると助かります。よろしくお願いします。
362
+
363
+ ### 補足
364
+
365
+ DBはusersテーブルとpostsテーブルがあります。
366
+
367
+ DBにはデータがきちんと格納されています。
368
+
369
+
370
+
371
+ usersテーブル
372
+
373
+ user_id(AI), username, email, password, photo
374
+
375
+ postsテーブル
376
+
377
+ id(AI), post_name, post_author, post_introduction, post_image, post_id