質問編集履歴
1
現在の状況・問題・コードのインデックス
title
CHANGED
File without changes
|
body
CHANGED
@@ -25,70 +25,35 @@
|
|
25
25
|
<?php
|
26
26
|
if (((isset($_POST["name"])) && ($_POST["name"] != "")) or ((isset($_POST["comment"])) && ($_POST["comment"] != "")))
|
27
27
|
{
|
28
|
-
if (isset($_POST["name"]))
|
28
|
+
if (isset($_POST["name"]))
|
29
|
-
{
|
29
|
+
{
|
30
|
-
$name = $_POST["name"];
|
30
|
+
$name = $_POST["name"];
|
31
|
-
if( get_magic_quotes_gpc() ) { $name = stripslashes("$name"); }
|
31
|
+
if( get_magic_quotes_gpc() ) { $name = stripslashes("$name"); }
|
32
|
-
$name = htmlspecialchars ($name);
|
32
|
+
$name = htmlspecialchars ($name);
|
33
|
-
$name = mb_substr ($name, 0, 30, 'UTF-8');
|
33
|
+
$name = mb_substr ($name, 0, 30, 'UTF-8');
|
34
|
-
}
|
34
|
+
}
|
35
|
-
if (isset($_POST["comment"]))
|
35
|
+
if (isset($_POST["comment"]))
|
36
|
-
{
|
36
|
+
{
|
37
|
-
$comment = $_POST["comment"];
|
37
|
+
$comment = $_POST["comment"];
|
38
|
-
if( get_magic_quotes_gpc() ) { $comment = stripslashes("$comment"); }
|
38
|
+
if( get_magic_quotes_gpc() ) { $comment = stripslashes("$comment"); }
|
39
|
-
$comment = mb_substr ($comment, 0, 200, 'UTF-8');
|
39
|
+
$comment = mb_substr ($comment, 0, 200, 'UTF-8');
|
40
|
-
}
|
40
|
+
}
|
41
|
-
if (isset($_POST["delete"]))
|
42
|
-
{
|
43
|
-
$delete = $_POST["delete"];
|
44
|
-
if( get_magic_quotes_gpc() ) { $delete = stripslashes("$delete"); }
|
45
|
-
$delete = htmlspecialchars ($delete);
|
46
|
-
}
|
47
41
|
|
48
|
-
|
49
|
-
$fp = fopen("write.txt", "r");
|
42
|
+
$fp = fopen("write.txt", "r");
|
50
|
-
$number = fgets($fp);
|
43
|
+
$number = fgets($fp);
|
51
|
-
$i = $number+1;
|
44
|
+
$i = $number+1;
|
52
|
-
fclose ($fp);
|
45
|
+
fclose ($fp);
|
53
|
-
$time = date("Y/n/j G:i");
|
46
|
+
$time = date("Y/n/j G:i");
|
54
|
-
$write = $i . "<>" . $name . "<>" . $comment . "<>" . $time .PHP_EOL ;
|
47
|
+
$write = $i . "<>" . $name . "<>" . $comment . "<>" . $time .PHP_EOL ;
|
55
|
-
$fp = fopen ("./log.txt","a+");
|
48
|
+
$fp = fopen ("./log.txt","a+");
|
56
|
-
flock ($fp, LOCK_EX);
|
49
|
+
flock ($fp, LOCK_EX);
|
57
|
-
fputs ($fp,$write);
|
50
|
+
fputs ($fp,$write);
|
58
|
-
flock ($fp, LOCK_UN);
|
51
|
+
flock ($fp, LOCK_UN);
|
59
|
-
fclose ($fp);
|
52
|
+
fclose ($fp);
|
60
53
|
}
|
54
|
+
|
61
55
|
?>
|
62
|
-
|
63
56
|
<?php
|
64
|
-
if((isset($_POST["delete"])) && ($_POST["delete"] !=""))
|
65
|
-
{
|
66
|
-
|
67
|
-
if(isset($_POST["delete"]))
|
68
|
-
{
|
69
|
-
$delete=$_POST["delete"];
|
70
|
-
$fp = fopen('log.txt', 'r');
|
71
|
-
while (!feof($fp)){
|
72
|
-
$line = fgets($fp);
|
73
|
-
$del = explode("<>",$line);
|
74
|
-
}
|
75
|
-
fclose($fp);
|
76
|
-
|
77
|
-
if($del[0] != $delete)
|
78
|
-
{
|
79
|
-
$fp = fopen('log.txt', 'w');
|
80
|
-
fwrite($fp,$line);
|
81
|
-
fclose($fp);
|
82
|
-
}
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
}
|
87
|
-
print "削除しました<br>";
|
88
|
-
}
|
89
|
-
?>
|
90
|
-
|
91
|
-
<?php
|
92
57
|
if (((isset($_POST["name"])) && ($_POST["name"] != "")) or ((isset($_POST["comment"])) && ($_POST["comment"] != "")))
|
93
58
|
{
|
94
59
|
|
@@ -113,4 +78,43 @@
|
|
113
78
|
}
|
114
79
|
fclose($fp);
|
115
80
|
?>
|
116
|
-
```
|
81
|
+
```
|
82
|
+
削除機能
|
83
|
+
```php
|
84
|
+
<?php
|
85
|
+
if((isset($_POST["delete"])) && ($_POST["delete"] !=""))
|
86
|
+
{
|
87
|
+
if (isset($_POST["delete"]))
|
88
|
+
{
|
89
|
+
$delete = $_POST["delete"];
|
90
|
+
if( get_magic_quotes_gpc() ) { $delete = stripslashes("$delete");}
|
91
|
+
$delete = htmlspecialchars ($delete);
|
92
|
+
}
|
93
|
+
|
94
|
+
$fp = fopen('log.txt', 'r');
|
95
|
+
while (!feof($fp))
|
96
|
+
{
|
97
|
+
$line = fgets($fp);
|
98
|
+
$del = explode("<>",$line);
|
99
|
+
}
|
100
|
+
fclose($fp);
|
101
|
+
|
102
|
+
if($del[0] != $delete)
|
103
|
+
{
|
104
|
+
$fp = fopen('log.txt', 'w');
|
105
|
+
fwrite($fp,$line);
|
106
|
+
fclose($fp);
|
107
|
+
}
|
108
|
+
print "削除しました<br>";
|
109
|
+
|
110
|
+
}
|
111
|
+
?>
|
112
|
+
|
113
|
+
|
114
|
+
```
|
115
|
+
追加・修正について
|
116
|
+
現在phpで簡易掲示板を作成しており、名前・コメントのいずれかの入力フォームから値が送信されると、ブラウザ上に"投稿番号<>名前<>コメント<>投稿時間"が表示されるようにしました。ここまでは実装することができました。
|
117
|
+
|
118
|
+
そこで次はこれらの投稿を削除する機能を実装したいです。具体的には、まず削除番号の項目のフォームから値が送信されるとPOSTで受け取り、その後、投稿内容が保存されている"log.txt"を読み込み、explode関数を用いて投稿番号を取得、行末までループさせます。その後取得した投稿番号と送信された値を比較して等しくない場合のみ投稿を"log.txt"に上書きすることによって、値と等しい投稿番号の書き込みを削除したいです。その削除機能を実装しようとしたのが上記です。
|
119
|
+
上記のコードではエラーは出ないのですが、送信しても何も起きず、printを用いて表示されるはずの"削除しました"も表示されません。ただ値を送信しただけになってしまいます。うまく値を受け取れていないような気がするのですが理由が分からない状態です。そのため、投稿番号を取得する部分や比較する部分のコードが正しいのかどうかも確認することができていないです。
|
120
|
+
インデックスはつけて修正しました。他にも読みづらい点があるとは思いますがどうかよろしくお願い致します。何か他にもありましたらご指摘よろしくお願い致します。
|