Home>

When you enter the number of the comment I want to delete, the comment with that number is deleted from the text file and displayed at the bottom of the input form.

If i try to execute the code that extracts only the part to delete the comment below, the comment I want to delete will be changed to "Deleted" and it will be reflected on the browser, but if you incorporate it in the entire code and execute it , There is no change in the text file and there is no change on the browser.
No error message has occurred.

Corresponding source code

Whole code

<? php
// Save the commented content to a text file
date_default_timezone_set ('Japan');
$name ='';
$comment ='';
$filepath ='keijiban.txt';
if (isset ($_ POST ['send'])) {
  $name = $_REQUEST ['name'];
  $comment = $_REQUEST ['comment'];
  $date = date ('Y/m/d H: i: s');
  if (($name =='') or ($comment =='')) {
      echo'There are unfilled items';
  } else {
    if (file_exists ($filepath)) {
      $num = count (file ($filepath)) + 1;
    } else {
      $num = 1;
    }
    $fp = fopen ($filepath,'a');
    fwrite ($fp, $num.'<>'. $Name.'<>'. $Comment.'<>'. $Date. "\ n");
    fclose ($fp);
  }
}
?>
<? php
// Delete comment
$del_num ='';
if (isset ($_ POST ['delsend'])) {
  $filedata = file ($filepath);
  $del_num = $_REQUEST ['delete'];
  if ($del_num =='') {
      echo'No number specified';
  } else {
    if (($del_num<= count ($filedata)) and ($del_num>0)) {
      $fp = fopen ($filepath,'w');
      for ($i = 0;$i<count ($filedata);$i ++) {
        $delcom = explode ('<>', $filedata [$i]);
        if ($delcom [0]! = $del_num) {
          fwrite ($fp, $filedata [$i]);
        } else {
          fwrite ($fp, "deleted \ n");
        }
      }
      fclose ($fp);
    } else {
      echo'The comment number does not exist';
    }
  }
}
?>
<! DOCTYPE html>
<html>
<head>
<meta charset ='utf-8'>
<title>Bulletin board</title>
</head>
<body>
<form action ='' method ='post'>
Name<br>
<input type ='text' name ='name'><br>
Comment<br>
<textarea name = "comment"></textarea><br>
<input type ='submit' name ='send' value ='submit'><br>
Comment number I want to delete<br>
<input type ='text' name ='delete'><br>
<input type ='submit' name ='delsend' value ='delete'>
</form>
 Comment section 
<? php
if (file_exists ($filepath)) {
  $file = file ($filepath);
  foreach ($file as $value) {
    $com = explode ('<>', $value);
    foreach ($com as $c) {
      echo $c;
    }
    echo "<br>";
  }
}
?>
</body>
</html>

↓ Code that extracts only the part to delete the comment ↓

<? php
$del_num = 2;
$filepath ='keijiban.txt';
$filedata = file ($filepath);
$fp = fopen ($filepath,'w');
for ($i = 0;$i<count ($filedata);$i ++) {
  $delcom = explode ('<>', $filedata [$i]);
  if ($delcom [0]! = $del_num) {
    fwrite ($fp, $filedata [$i]);
  } else {
    fwrite ($fp, "deleted \ n");
  }
}
fclose ($fp);
$file = file ($filepath);
foreach ($file as $value) {
  $com = explode ('<>', $value);
  foreach ($com as $c) {
    echo $c;
  }
  echo "<br>";
}
What I tried

I had a lot of similar questions, so I looked at them and fixed them.

Supplementary information (FW/tool version, etc.)

macOS
I am creating in a local environment using XAMPP

The process of the comment deletion part extracts the number at the beginning of the comment, replaces it with "deleted" if the number and the entered number ($del_num) match, and if not, puts the original comment in the file. I am writing
The processing when the number is not entered in the comment deletion field and the processing when a number larger than the number of comments is entered are working well.

php
  • Answer # 1

    To make it "deleted", the conditional expression written during loop processing with for

    if ($delcom [0]! = $del_num) {


    so$delcom [0]It seems that the cause is that the space is included in.

    if (trim ($delcom [0])! = $del_num) {


    If so, it will be solved for the time being.

    (Although it deviates from the main subject, it is not good for security to use the value obtained by $_POST as it is, so be sure to check the value and handle it as data if necessary. Temporarily comment column You can see it by putting a line break in the text or putting an html tag in the post.)