Please write your question in detail here.
In the process of creating a simple bulletin board with php, I am trying to implement a function to edit posts, but I am stuck because I do not know how to display the name and comment that match the obtained post number in the form. Here, I would like to edit the name and comment that match the post number and overwrite it to make it a function called editing.
The flow is as follows
[Prepare "Edit number specification form" alongside "Input form" and "Delete form" -Enter "Edit target number" and have one "Edit" button]
[Send "Edit target number" by POST transmission. When receiving, branch the process with "edit form" separately from each existing form with if statement]
[Use the file read function to assign the contents of the file to the array variable as one element per line. Open the file and loop by the number of elements (= number of lines) in the array.
[In the loop process: Divide by the delimiter "<>" to get the post number]
[Same as in loop processing: Compare post number and edit target number. If equal, get the "name" and "comment" of the post]
[Display the existing post form with the contents of the "name" and "comment" obtained above already included]
[Add information to determine "new post or edit (new registration mode/edit mode) if sent now" in the existing post form]
Since the post form doubles as "new post" and "edit", it is necessary to be able to judge "new post or edit".
As a process of creation, prepare a new item (text box *) in the form so that the post number I want to edit is displayed there.
(* This will eventually be invisible to the user).
[If there is no problem with the edited content: Compare the post number and the edit target number, and if they are the same, replace the content written in the file with the transmitted content]
If there is a transmission from this form
-Check if the above text box is empty:
-If it is not empty, take out the contents of the text file and compare the post numbers of each line.
-Only when they match, replace them with the values ​​sent from the edit form.
・ When it is empty, it will be treated as a normal new post.
[If the information added to the form above is visible from the browser, change the type attribute to hidden to make it invisible]

Error message
Corresponding source code
<html><head><meta charset = "UTF-8"><title>mission3-5</title></head><body><form action = "" method = "post">    <span>[Posting form]</span><br>    <span>Name:</span>    <input type = "text" name = "name"><br>    <span>Comment:</span>    <input type = "text" name = "comment"><br>    <span>Password:</span>    <input type = "text" name = "password"><br>    <input type = "submit" value = "submit">    <span>[Delete form]</span><br>    <span>Post number:</span>    <input type = "number" name = "delete"><br>    <span>Password:</span>    <input type = "text" name = "password"><br>    <input type = "submit" value = "delete"><span>[Edit form]</span><br>    <span>Post number:</span>    <input type = "number" name = "edit"><br>    <span>Password:</span>    <input type = "text" name = "password"><br>    <input type = "submit" value = "edit"><br>    <? php
        $name = $_POST ["name"];
        $delete = $_POST ["delete"];
        $comment = $_POST ["comment"];
        $edit = $_POST ["edit"];
        $dates = date ('Y year m month d day H hours i minutes s seconds');
        $filename = "mission3-5.txt";
        echo "----------------". "<br>". "<br>";
        if (empty ($name)) {
            echo "Error: name is empty";
        elseif (empty ($comment)) {
            echo "Error: comment is empty";
        elseif (empty ($password)) {
            echo "Error: password is empty";
        echo "<br>". "<br>". "----------------". "<br>";
        echo "----------------------". "<br>". "[Post list]". "<br>";
        if (! empty ($name)&&! empty ($comment)) {
            $p = fopen ($filename, "a");
            $total = (count (file ($filename)) + 1). "<>". $Name. "<>". $Comment. "<>". $Dates.PHP_EOL;
            fwrite ($p, $total);
            fclose ($p);
            $lines = file ($filename);
            foreach ($lines as $line) {
                $contents = explode ("<>", $line);
                $postnumber = $contents [0];
                $fullnamer = $contents [1];
                $commenter = $contents [2];
                $dater = $contents [3];
                echo $postnumber;
                echo $fullnamer;
                echo $commenter;
                echo $dater. "<br>";}}
        elseif (isset ($delete)) {
            $delcon = file ($filename);// Specify the text file to open with the file function
                $fp = fopen ($filename, "w");for ($j = 0;$j<count ($delcon);$j ++) {
                $deldata = explode ("<>", $delcon [$j]);
                if ($deldata [0]! = $delete) {
                fwrite ($fp, $delcon [$j]);
                } else {
                fwrite ($fp, "");
            $lines = file ($filename);
            foreach ($lines as $line) {
                $contents = explode ("<>", $line);
                $postnumber = $contents [0];
                $fullnamer = $contents [1];
                $commenter = $contents [2];
                $dater = $contents [3];
                echo $postnumber;
                echo $fullnamer;
                echo $commenter;
                echo $dater. "<br>";}
        if (isset ($edit)) {
        // This is where I want the data from the edit number
        // Process the number of data items
        $lines = file ($filename);
        foreach ($lines as $row) {
            // Split with<>into an array
            $bbsRowData = explode ("<>", $row);
            // Set the data when it is the edit target number
            if ($bbsRowData [0] == $_POST ["number"]) {
                $editNumber = $bbsRowData [0];
                $editName = $bbsRowData [1];
                $editComment = $bbsRowData [2];
                // Exit immediately

What I tried

Please describe here what you have tried for the problem.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    Display the name and comment that match the retrieved post number in the form

    For the time being, I will explain the procedure to achieve only this point. Please forgive me if the layout of the page is broken (post list comes to the top of the page, etc.).

    First,From?>Move up to the beginning of the source file.

    next,$editCommentOf the part to set etc.ifI think the sentence is wrong, so let's correct it.
    The parameter name of the post number isnumbernotedit editis not it.

    if ($bbsRowData [0] == $_POST ["edit"]) {

    Finally, in the edit forminputThe data to be edited is output to the element as the initial value.
    For example, in the comment section, it looks like this.

    <input type = "text" name = "comment" value = "<? php echo $editComment ??'';?>"><br>

    * HTML escape is omitted (don't copy it).