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

質問編集履歴

1

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

2019/07/08 08:59

投稿

toki38
toki38

スコア14

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  ### 問題点
2
- 入力画面でtextareaに入力したテキストを改行すると、表示画面で改行部分が\r\nとでてきてしまう。
2
+ 入力画面(post.php)でtextareaに入力したテキストを改行すると、表示画面(home.php)で改行部分が\r\nとでてきてしまう。
3
3
 
4
4
  )例
5
5
  改行したい
@@ -31,4 +31,159 @@
31
31
 
32
32
  改行したい
33
33
 
34
+ ### コード
35
+ ```
36
+ (post.php)
37
+ <?php
38
+ session_start();
39
+  //DB接続
40
+ include_once('dbconnect.php');
41
+
42
+ if (!isset($_SESSION['user'])) {
43
+ header('location:login.php');
44
+ }
45
+
46
+ //投稿するボタンを押したときの処理
47
+ if (isset($_POST['posting'])) {
48
+ $post_name = $mysqli->real_escape_string($_POST['post_name']);
49
+ $post_author = $mysqli->real_escape_string($_POST['post_author']);
50
+ $post_introduction = $mysqli->real_escape_string($_POST['post_introduction']);
51
+ $post_image = "bookimage/".$_FILES['post_image']['name'];
52
+ //uses_idをpost_idに代入
53
+ $query2 = "SELECT user_id FROM users WHERE user_id=".$_SESSION['user']."";
54
+ $result = $mysqli->query($query2);
55
+ while ($row = $result->fetch_assoc()) {
56
+ $post_id = $row['user_id'];
57
+ }
58
+ //POSTされた情報をDBへ格納する
59
+ $query = "INSERT INTO posts SET post_name=?, post_author=?, post_introduction=?, post_image=?, post_id=?";
60
+ $stmt = $mysqli->prepare($query);
61
+ $stmt->bind_param("ssssi", $post_name, $post_author, $post_introduction, $post_image, $post_id);
62
+ $stmt->execute();
63
+ move_uploaded_file($_FILES['post_image']['tmp_name'], $post_image);
64
+
65
+ }
66
+
67
+ ?>
68
+
69
+ <!doctype html>
70
+ <html lang="ja">
71
+ <head>
72
+ <meta charset="utf-8">
73
+ 〜省略(Bootstrap使ってます)〜
74
+ </head>
75
+ <body>
76
+
77
+ <!-- Posting-wrapper -->
78
+ <div class="posting-wrapper py-5">
79
+ <div class="container">
80
+ <div class="row">
81
+ <div class="col-md-6 offset-md-3 text-center">
82
+ <h2 class="mb-5 p_title">投稿ページ</h2>
83
+ <form method="post" enctype="multipart/form-data">
84
+ <label class="col-form-label">タイトル <span class="bg-danger text-white p-1" >必須</span></label>
85
+ <input type="text" name="post_name" class="form-control" required/>
86
+ <label class="col-form-label">作者 <span class="bg-danger text-white p-1" >必須</span></label>
87
+ <input type="text" name="post_author" class="form-control" required/>
88
+ <label class="col-form-label">画像 <span class="bg-danger text-white p-1" >必須</span></label>
89
+ <input type="file" name="post_image" class="form-control" required/>
90
+ <label for="exampleTextarea">紹介文</label>
91
+ <!-- 質問の部分 -->
92
+ <textarea name="post_introduction" class="form-control" rows="8" cols="80"></textarea>
93
+          <!-- 質問ここまで -->
94
+ <div class="text-center">
95
+ <input type="submit" name="posting" class="btn btn-lg btn-success mt-4" value="投稿する">
96
+ </div>
97
+ </form>
98
+ </div>
99
+ </div>
100
+ </div>
101
+ </div>
102
+ <!-- End Posting-wrapper -->
103
+
104
+ </body>
105
+ </html>
106
+
107
+ ```
108
+
109
+ ```
110
+ (home.php)
111
+ <?php
112
+ session_start();
113
+  //DB接続
114
+ include_once('dbconnect.php');
115
+
116
+ if (!isset($_SESSION['user'])) {
117
+ header('location:login.php');
118
+ }
119
+
120
+  //usersテーブルとpostsテーブルをリレーションさせる
121
+ $query = "SELECT * FROM posts LEFT JOIN users ON posts.post_id = users.user_id ORDER BY id DESC";
122
+ $result = $mysqli->query($query);
123
+ // 結果を受け取る変数を配列にする
124
+ $posteds = [];
125
+ while ($row = $result->fetch_assoc()) {
126
+ // 配列に取り出した一行分の結果を足していく
127
+ $posteds[] = $row;
128
+ }
129
+
130
+ ?>
131
+
132
+ <!doctype html>
133
+ <html lang="ja">
134
+ <head>
135
+ <meta charset="utf-8">
136
+ 〜省略(Bootstrap使ってます)〜
137
+ </head>
138
+ <body>
139
+
140
+ <!-- top-wrapper -->
141
+ <div class="top-wrapper text-center text-white">
142
+ <a href="post.php" class="btn btn-lg btn-success mt-3"><span><i class="fas fa-book-open"></i></span> 投稿ページへ</a>
143
+ </div>
144
+ <!-- End top-wrapper -->
145
+
146
+ <!-- post-wrapper -->
147
+ <div class="post-wrapper pt-5">
148
+ <div class="container">
149
+ <div class="row">
150
+ <!-- 投稿を繰り返し表示 -->
151
+ <?php foreach ($posteds as $posted): ?>
152
+ <div class="col-md-4 text-center mb-3">
153
+ <img src="<?php echo $posted['post_image']; ?>" width="200px" height="300px">
154
+ </div>
155
+ <div class="col-md-8 mb-3 manga_data">
156
+ <h3><?php echo $posted['post_name']; ?></h3>
157
+ <p><?php echo $posted['post_author']; ?></p>
158
+        <!-- 質問の部分 -->
159
+ <p><?php echo nl2br(htmlspecialchars($posted['post_introduction'])); ?></p>
160
+        <!-- 質問ここまで -->
161
+ </div>
162
+ <div class="col-md-4 text-center mb-3">
163
+ <img src="<?php echo $posted['photo']; ?>" width="120px" height="120px" class="rounded-circle">
164
+ <p>紹介者 : <?php echo $posted['username']; ?>さん</p>
165
+ </div>
166
+ <div class="col-12">
167
+ <hr style="background-color:#EB6964" class="mb-4" >
168
+ </div>
169
+ <?php endforeach; ?>
170
+ <!-- ここまで繰り返し -->
171
+ </div>
172
+ </div>
173
+ </div>
174
+ <!-- End post-wrapper -->
175
+
176
+ </body>
177
+ </html>
178
+
179
+
180
+ ```
34
- ご助言頂けると助かります。よろしくお願いします。
181
+ ご助言頂けると助かります。よろしくお願いします。
182
+ ### 補足
183
+ DBはusersテーブルとpostsテーブルがあります。
184
+ DBにはデータがきちんと格納されています。
185
+
186
+ usersテーブル
187
+ user_id(AI), username, email, password, photo
188
+ postsテーブル
189
+ id(AI), post_name, post_author, post_introduction, post_image, post_id