Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以php格式将输入的数据保存到文本文件(if语句)_Php - Fatal编程技术网

如何以php格式将输入的数据保存到文本文件(if语句)

如何以php格式将输入的数据保存到文本文件(if语句),php,Php,我正在努力编写代码,因为这比让表单将数据保存到文本文件有点复杂。我希望表单显示有条件的消息,例如,如果提交了空表单,则会显示“请输入注释”,如果输入完成,则会显示数据已被接受,并且会显示“祝贺您”。如果是任何其他数据,它将简单地说数据已收到,并且将出现“感谢您的评论”。我能够做到这一点,但由于某种原因,我无法将php文件中输入的数据保存到文本文件中。这是我的密码: <!DOCTYPE html> <html lang="ja"> <hea

我正在努力编写代码,因为这比让表单将数据保存到文本文件有点复杂。我希望表单显示有条件的消息,例如,如果提交了空表单,则会显示
“请输入注释”
,如果输入完成,则会显示数据已被接受,并且会显示
“祝贺您”
。如果是任何其他数据,它将简单地说数据已收到,并且将出现
“感谢您的评论”
。我能够做到这一点,但由于某种原因,我无法将php文件中输入的数据保存到文本文件中。这是我的密码:

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset ="UTF-8">
        <title>mission_2-02</title>
    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="comment" placeholder="comment">
            <input type="submit" name="submit">
        </form>

        <?php
            $str = $comment . PHP_EOL;
            $filename="mission_2-02.txt";
            $fp = fopen($filename,"w");

            $comment = $_POST ["comment"];
            if ($comment==""){echo "";}
            else echo $comment . "is accepted <br>";


            if (empty ($comment)) {echo"please enter comment";}
            elseif ($comment=="finished"){echo "congratulations";}
            elseif (!empty($comment)){echo "thank you for the comment";}
            else{fwrite($fp, $str );echo $str ;fclose($fp);}
            
            
            if (file_exists($filename)){
                $lines = file($filename,FILE_IGNORE_NEW_LINES);
                foreach ($lines as $line){
                    echo $line . "<br>";
                }  
            }
        ?>
    </body>
</html>

任务2-02

使用有效的工具来处理PHP,并使用有效的格式以保持清晰,这将有助于加快速度

  • 在定义变量之前不能赋值(正如Abronsius教授所指出的)
  • 您试图在
    else
    子句中写入未到达的文件内容
  • 比较:

    
    
    顺便说一句,这种方法每次都覆盖整个文件,除了附加新值

    提示:虽然使用<代码>如果/代码>/<代码> EX/<代码>无括号的语句在PHP中是有效的,但考虑下面的代码:

    不安全。
    //方法不安全,尽管仍然有效,
    //在这种情况下,最后一个echo来自else语句!
    如果(1==1)
    回声“相等”;
    其他的
    回声“不相等”;
    呼应“其他东西”;
    //安全接近
    如果(1==1){
    回声“相等”;
    }
    否则{
    回声“不相等”;
    呼应“其他东西”;
    }
    //短有效方法
    回声(1==1)?'相等:'不相等';
    
    主题的变体可能

    <!DOCTYPE html>
    <html lang="ja">
        <head>
            <meta charset='UTF-8' />
            <title>mission_2-02</title>
        </head>
        <body>
            <form method='post'>
                <input type='text' name='comment' placeholder='comment' />
                <input type='submit' />
            </form>
    
            <?php
                
                $filename='mission_2-02.txt';
                
                if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST["comment"] ) ){
                    
                    $output=array();
                    $errors=array();
                    
                    $comment=filter_input( INPUT_POST, 'comment', FILTER_SANITIZE_STRING );
                    
                    
                    if( !empty( $comment ) )$output[]=sprintf('Thankyou - your comment "%s" was accepted!', $comment );
                    else $errors[]='Please enter a comment!';
                    
                    if( strtolower( trim( $comment ) )==='finished' )$errors[]='Congratulations';
                    
                    if( !empty( $errors ) )printf('<pre>%s</pre>',implode( PHP_EOL, $errors ));
                    else{
                        file_put_contents( $filename, $comment . PHP_EOL, FILE_APPEND );
                        printf('<pre>%s</pre>',implode( PHP_EOL, $output ) );
                    }
                }
                
                
                if( file_exists( $filename ) ){
                    foreach( file( $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) as $line )printf('%s<br />',$line);
                }
    
                clearstatcache();
            ?>
        </body>
    </html>
    
    
    任务2-02
    
    您的HTML标记无效。结束
    必须在开始
    标记之前,并且
    中的
    字符集
    之间应该有一个空格。看起来这个问题与MySqlYou在定义变量之前分配变量-
    $str=$comment无关。PHP_EOL~在该阶段尚未创建