質問編集履歴

1

現在の状況・問題・コードのインデックス

2017/08/30 14:57

投稿

tatsu31
tatsu31

スコア12

test CHANGED
File without changes
test CHANGED
@@ -52,75 +52,117 @@
52
52
 
53
53
  {
54
54
 
55
- if (isset($_POST["name"]))
56
-
57
- {
58
-
59
- $name = $_POST["name"];
60
-
61
- if( get_magic_quotes_gpc() ) { $name = stripslashes("$name"); }
62
-
63
- $name = htmlspecialchars ($name);
64
-
65
- $name = mb_substr ($name, 0, 30, 'UTF-8');
66
-
67
- }
68
-
69
- if (isset($_POST["comment"]))
70
-
71
- {
72
-
73
- $comment = $_POST["comment"];
74
-
75
- if( get_magic_quotes_gpc() ) { $comment = stripslashes("$comment"); }
76
-
77
- $comment = mb_substr ($comment, 0, 200, 'UTF-8');
78
-
79
- }
80
-
81
- if (isset($_POST["delete"]))
82
-
83
- {
84
-
85
- $delete = $_POST["delete"];
86
-
87
- if( get_magic_quotes_gpc() ) { $delete = stripslashes("$delete"); }
88
-
89
- $delete = htmlspecialchars ($delete);
90
-
91
- }
92
-
93
-
94
-
95
-
96
-
97
- $fp = fopen("write.txt", "r");
98
-
99
- $number = fgets($fp);
100
-
101
- $i = $number+1;
102
-
103
- fclose ($fp);
104
-
105
- $time = date("Y/n/j G:i");
106
-
107
- $write = $i . "<>" . $name . "<>" . $comment . "<>" . $time .PHP_EOL ;
108
-
109
- $fp = fopen ("./log.txt","a+");
110
-
111
- flock ($fp, LOCK_EX);
112
-
113
- fputs ($fp,$write);
114
-
115
- flock ($fp, LOCK_UN);
116
-
117
- fclose ($fp);
118
-
119
- }
120
-
121
- ?>
122
-
123
-
55
+ if (isset($_POST["name"]))
56
+
57
+ {
58
+
59
+ $name = $_POST["name"];
60
+
61
+ if( get_magic_quotes_gpc() ) { $name = stripslashes("$name"); }
62
+
63
+ $name = htmlspecialchars ($name);
64
+
65
+ $name = mb_substr ($name, 0, 30, 'UTF-8');
66
+
67
+ }
68
+
69
+ if (isset($_POST["comment"]))
70
+
71
+ {
72
+
73
+ $comment = $_POST["comment"];
74
+
75
+ if( get_magic_quotes_gpc() ) { $comment = stripslashes("$comment"); }
76
+
77
+ $comment = mb_substr ($comment, 0, 200, 'UTF-8');
78
+
79
+ }
80
+
81
+
82
+
83
+ $fp = fopen("write.txt", "r");
84
+
85
+ $number = fgets($fp);
86
+
87
+ $i = $number+1;
88
+
89
+ fclose ($fp);
90
+
91
+ $time = date("Y/n/j G:i");
92
+
93
+ $write = $i . "<>" . $name . "<>" . $comment . "<>" . $time .PHP_EOL ;
94
+
95
+ $fp = fopen ("./log.txt","a+");
96
+
97
+ flock ($fp, LOCK_EX);
98
+
99
+ fputs ($fp,$write);
100
+
101
+ flock ($fp, LOCK_UN);
102
+
103
+ fclose ($fp);
104
+
105
+ }
106
+
107
+
108
+
109
+ ?>
110
+
111
+ <?php
112
+
113
+ if (((isset($_POST["name"])) && ($_POST["name"] != "")) or ((isset($_POST["comment"])) && ($_POST["comment"] != "")))
114
+
115
+ {
116
+
117
+
118
+
119
+ $fp = fopen('write.txt', 'r');
120
+
121
+ $number = fgets($fp);
122
+
123
+ $i = $number+1;
124
+
125
+ fclose($fp);
126
+
127
+ $fp = fopen('write.txt', 'w');
128
+
129
+ fputs($fp, $i);
130
+
131
+ fclose($fp);
132
+
133
+ print "書き込み完了しました<br>";
134
+
135
+ }
136
+
137
+ ?>
138
+
139
+
140
+
141
+ <?php
142
+
143
+ $fp = fopen('log.txt', 'r');
144
+
145
+ while (!feof($fp))
146
+
147
+ {
148
+
149
+ $line = fgets($fp);
150
+
151
+ print $line;
152
+
153
+ print "<br>\n";
154
+
155
+ }
156
+
157
+ fclose($fp);
158
+
159
+ ?>
160
+
161
+ ```
162
+
163
+ 削除機能
164
+
165
+ ```php
124
166
 
125
167
  <?php
126
168
 
@@ -128,104 +170,70 @@
128
170
 
129
171
  {
130
172
 
131
-
132
-
133
- if(isset($_POST["delete"]))
173
+ if (isset($_POST["delete"]))
134
-
174
+
135
- {
175
+ {
136
-
176
+
137
- $delete=$_POST["delete"];
177
+ $delete = $_POST["delete"];
178
+
138
-
179
+ if( get_magic_quotes_gpc() ) { $delete = stripslashes("$delete");}
180
+
181
+ $delete = htmlspecialchars ($delete);
182
+
183
+ }
184
+
185
+
186
+
139
- $fp = fopen('log.txt', 'r');
187
+ $fp = fopen('log.txt', 'r');
140
-
188
+
141
- while (!feof($fp)){
189
+ while (!feof($fp))
190
+
142
-
191
+ {
192
+
143
- $line = fgets($fp);
193
+ $line = fgets($fp);
144
-
194
+
145
- $del = explode("<>",$line);
195
+ $del = explode("<>",$line);
146
-
196
+
147
- }
197
+ }
148
-
149
- fclose($fp);
150
-
151
-
152
-
153
- if($del[0] != $delete)
154
-
155
- {
156
-
157
- $fp = fopen('log.txt', 'w');
158
-
159
- fwrite($fp,$line);
160
-
161
- fclose($fp);
162
-
163
- }
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
- }
172
-
173
- print "削除しました<br>";
174
-
175
- }
176
-
177
- ?>
178
-
179
-
180
-
181
- <?php
182
-
183
- if (((isset($_POST["name"])) && ($_POST["name"] != "")) or ((isset($_POST["comment"])) && ($_POST["comment"] != "")))
184
-
185
- {
186
-
187
-
188
-
189
- $fp = fopen('write.txt', 'r');
190
-
191
- $number = fgets($fp);
192
-
193
- $i = $number+1;
194
198
 
195
199
  fclose($fp);
196
200
 
201
+
202
+
203
+ if($del[0] != $delete)
204
+
205
+ {
206
+
197
- $fp = fopen('write.txt', 'w');
207
+ $fp = fopen('log.txt', 'w');
198
-
208
+
199
- fputs($fp, $i);
209
+ fwrite($fp,$line);
200
-
210
+
201
- fclose($fp);
211
+ fclose($fp);
202
-
203
- print "書き込み完了しました<br>";
212
+
204
-
205
- }
213
+ }
214
+
206
-
215
+ print "削除しました<br>";
216
+
217
+
218
+
219
+ }
220
+
207
- ?>
221
+ ?>
208
-
209
-
210
-
211
- <?php
222
+
212
-
213
- $fp = fopen('log.txt', 'r');
223
+
214
-
215
- while (!feof($fp))
224
+
216
-
217
- {
225
+
218
-
219
- $line = fgets($fp);
220
-
221
- print $line;
222
-
223
- print "<br>\n";
224
-
225
- }
226
-
227
- fclose($fp);
228
-
229
- ?>
230
226
 
231
227
  ```
228
+
229
+ 追加・修正について
230
+
231
+ 現在phpで簡易掲示板を作成しており、名前・コメントのいずれかの入力フォームから値が送信されると、ブラウザ上に"投稿番号<>名前<>コメント<>投稿時間"が表示されるようにしました。ここまでは実装することができました。
232
+
233
+
234
+
235
+ そこで次はこれらの投稿を削除する機能を実装したいです。具体的には、まず削除番号の項目のフォームから値が送信されるとPOSTで受け取り、その後、投稿内容が保存されている"log.txt"を読み込み、explode関数を用いて投稿番号を取得、行末までループさせます。その後取得した投稿番号と送信された値を比較して等しくない場合のみ投稿を"log.txt"に上書きすることによって、値と等しい投稿番号の書き込みを削除したいです。その削除機能を実装しようとしたのが上記です。
236
+
237
+ 上記のコードではエラーは出ないのですが、送信しても何も起きず、printを用いて表示されるはずの"削除しました"も表示されません。ただ値を送信しただけになってしまいます。うまく値を受け取れていないような気がするのですが理由が分からない状態です。そのため、投稿番号を取得する部分や比較する部分のコードが正しいのかどうかも確認することができていないです。
238
+
239
+ インデックスはつけて修正しました。他にも読みづらい点があるとは思いますがどうかよろしくお願い致します。何か他にもありましたらご指摘よろしくお願い致します。