さきほ No title
No License PHP
2020年09月15日
Copy
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>mission_3-5</title>
</head>
<body>
    
    <?php
        $filename = "mission_3-5.txt";
    /*送信*/
        $name = $_POST["name"];
        $comment = $_POST["comment"];
        $pass = $_POST["password"];
        $delete = $_POST["delete"];
        $del_pass = $_POST["delete_pass"];
        $edit = $_POST["edit"];
        $editNo = $_POST["editNo"];
        $edit_pass = $_POST["edit_pass"];
    /*時間取得*/
        $date = date("Y/m/d H:i:s");
 
//編集番号が送信されていて、ファイルが存在する時
        if(!empty($edit) && !empty($edit_pass) && file_exists($filename)){
            $edit_lines = file($filename);
            foreach($edit_lines as $edit_line){
                $edit_line_div = explode("<>", $edit_line);
                if($edit == $edit_line_div[0] && $edit_pass == $edit_line_div[4]){
                    $editname = $edit_line_div[1];
                    $editcom = $edit_line_div[2];
                }
            }
            
        }
   
//名前とコメントがあるなら       
        if(!empty($name && $comment && $pass)){
            if(!empty($editNo)){ 
                $lines = file($filename);
                //書き込みモードにしてテキストファイルを書き直す
                $edit_fp = fopen($filename, "w");
                
                foreach($lines as $line){
                    $line_div = explode("<>", $line);
                    if($line_div[0] != $editNo){
                        fwrite($edit_fp, $line);
                    }
                    else{
                        $num = $editNo;
                        fwrite($edit_fp,
                               $num."<>".$name."<>".$comment."<>".$date."<>".$pass."<>".PHP_EOL);
                    }
                }
                fclose($edit_fp);
            }
            else{
                $fp = fopen($filename, "a");
                if(file_exists($filename)){
                    $num = count(file($filename))+1;
                }
                else{
                    $num = 1;
                }
                $newdata = ($num."<>".$name."<>".$comment."<>".$date."<>".$pass."<>".PHP_EOL);
                fwrite($fp, $newdata);
                fclose($fp);
            } 
        }
        

//削除番号が入力されていて、ファイルが存在する時
        if(!empty($delete) && !empty($del_pass) && file_exists($filename)){
            $delete_lines = file($filename);
            $del_fp = fopen($filename, "w");
            foreach($delete_lines as $delete_line){
                $delete_line_div = explode("<>", $delete_line);
                if($delete == $delete_line_div[0] && $del_pass == $delete_line_div[4]){
                    fwrite($del_fp, "削除済み\n");
                }
                else{
                    fwrite($del_fp,$delete_line);
                }
            }
            fclose($del_fp);
        }
    ?>
        
    <form action="" method="post">
    <!-- 入力フォーム -->
        <input type="text" name="name" placeholder="名前" 
         value="<?php if($editname){echo $editname;}?>">
        <input type="text" name="comment" placeholder="コメント"
         value="<?php if($editcom){echo $editcom;}?>">
        <input type="hidden" name="editNo" 
         value="<?php if($edit){echo $edit;}?>">
        <input type="text" name="password" placeholder="パスワード">
        <input type="submit" name="submit">
        <br>
    <!-- 削除フォーム -->
        <input type="number" name="delete" placeholder="削除番号" value="">
        <input type="text" name="delete_pass" placeholder="パスワード">
        <input type="submit" name="deleteNo" value="削除">
        
        <br>
    <!-- 編集フォーム -->
        <input type="number" name="edit" placeholder="編集番号" value="">
        <input type="text" name="edit_pass" placeholder="パスワード">
        <input type="submit" name="editsub" value="編集">

    </form>
        
    <?php  
        if (file_exists($filename)) {
            $arrays = file($filename,FILE_IGNORE_NEW_LINES);
            foreach($arrays as $var){
            $data= explode("<>",$var);
     
            echo $data[0]." ";
            echo $data[1]." ";
            echo $data[2]." ";
            echo $data[3]."<br>";
            }
        }
        
        
    ?>


</body> 
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>mission_3-5</title>
</head>
<body>
    
    <?php
        $filename = "mission_3-5.txt";
    /*送信*/
        $name = $_POST["name"];
        $comment = $_POST["comment"];
        $pass = $_POST["password"];
        $delete = $_POST["delete"];
        $del_pass = $_POST["delete_pass"];
        $edit = $_POST["edit"];
        $editNo = $_POST["editNo"];
        $edit_pass = $_POST["edit_pass"];
    /*時間取得*/
        $date = date("Y/m/d H:i:s");
 
//編集番号が送信されていて、ファイルが存在する時
        if(!empty($edit) && !empty($edit_pass) && file_exists($filename)){
            $edit_lines = file($filename);
            foreach($edit_lines as $edit_line){
                $edit_line_div = explode("<>", $edit_line);
                if($edit == $edit_line_div[0] && $edit_pass == $edit_line_div[4]){
                    $editname = $edit_line_div[1];
                    $editcom = $edit_line_div[2];
                }
            }
            
        }
   
//名前とコメントがあるなら       
        if(!empty($name && $comment && $pass)){
            if(!empty($editNo)){ 
                $lines = file($filename);
                //書き込みモードにしてテキストファイルを書き直す
                $edit_fp = fopen($filename, "w");
                
                foreach($lines as $line){
                    $line_div = explode("<>", $line);
                    if($line_div[0] != $editNo){
                        fwrite($edit_fp, $line);
                    }
                    else{
                        $num = $editNo;
                        fwrite($edit_fp,
                               $num."<>".$name."<>".$comment."<>".$date."<>".$pass."<>".PHP_EOL);
                    }
                }
                fclose($edit_fp);
            }
            else{
                $fp = fopen($filename, "a");
                if(file_exists($filename)){
                    $num = count(file($filename))+1;
                }
                else{
                    $num = 1;
                }
                $newdata = ($num."<>".$name."<>".$comment."<>".$date."<>".$pass."<>".PHP_EOL);
                fwrite($fp, $newdata);
                fclose($fp);
            } 
        }
        

//削除番号が入力されていて、ファイルが存在する時
        if(!empty($delete) && !empty($del_pass) && file_exists($filename)){
            $delete_lines = file($filename);
            $del_fp = fopen($filename, "w");
            foreach($delete_lines as $delete_line){
                $delete_line_div = explode("<>", $delete_line);
                if($delete == $delete_line_div[0] && $del_pass == $delete_line_div[4]){
                    fwrite($del_fp, "削除済み\n");
                }
                else{
                    fwrite($del_fp,$delete_line);
                }
            }
            fclose($del_fp);
        }
    ?>
        
    <form action="" method="post">
    <!-- 入力フォーム -->
        <input type="text" name="name" placeholder="名前" 
         value="<?php if($editname){echo $editname;}?>">
        <input type="text" name="comment" placeholder="コメント"
         value="<?php if($editcom){echo $editcom;}?>">
        <input type="hidden" name="editNo" 
         value="<?php if($edit){echo $edit;}?>">
        <input type="text" name="password" placeholder="パスワード">
        <input type="submit" name="submit">
        <br>
    <!-- 削除フォーム -->
        <input type="number" name="delete" placeholder="削除番号" value="">
        <input type="text" name="delete_pass" placeholder="パスワード">
        <input type="submit" name="deleteNo" value="削除">
        
        <br>
    <!-- 編集フォーム -->
        <input type="number" name="edit" placeholder="編集番号" value="">
        <input type="text" name="edit_pass" placeholder="パスワード">
        <input type="submit" name="editsub" value="編集">

    </form>
        
    <?php  
        if (file_exists($filename)) {
            $arrays = file($filename,FILE_IGNORE_NEW_LINES);
            foreach($arrays as $var){
            $data= explode("<>",$var);
     
            echo $data[0]." ";
            echo $data[1]." ";
            echo $data[2]." ";
            echo $data[3]."<br>";
            }
        }
        
        
    ?>


</body> 
</html>

Output

    
Run
No one still commented. Please first comment.