【问题标题】:PHP saving a string to a filePHP 将字符串保存到文件中
【发布时间】:2014-02-11 15:19:12
【问题描述】:

感谢 enijar 发现了第一个错误,但现在我看到它没有向我的文件写入任何内容。再说一遍,不知道发生了什么。

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $fp = fopen("demoFormData.txt", "a");
        $savestring = clean_string($name).", ".clean_string($company).", ".clean_string($email).", ".$model.", ".$os.", ".$comments."\n"."-----------------------------------\n";
        fwrite($fp, $savestring);
        fclose($fp);

        echo 'form saved';

这是我所有的相关代码,包括错误检查:

if(isset($_POST["submit"])){
    function died($error) {
        // your error code can go here
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }

        if(!isset($_POST['name']) ||
            !isset($_POST['company']) ||
            !isset($_POST['email']) ||
            !isset($_POST['model']) ||
            !isset($_POST['OS']) ||
            !isset($_POST['comments'])) {

            died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }

        $name = $_POST["name"];
        $company = $_POST["company"];
        $email = $_POST["email"];
        $model = $_POST["model"];
        $os = $_POST["OS"];
        $comments = $_POST["comments"];

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

        if(!preg_match($email_exp, $email)){
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }

        $string_exp = "/^[A-Za-z .'-]+$/";
        if(!preg_match($string_exp, $name)){
            $error_message .= 'The Name you entered does not appear to be valid.<br/>';
        }
        if(!preg_match($string_exp, $company)){
            $error_message .= 'The Company you entered does not appear to be valid.<br/>';
        }
        if(strlen($comments) < 2) {
            $error_message .= 'The Comments you entered do not appear to be valid.<br />';
        }

        if(strlen($error_message) > 0) {
            died($error_message);
        }

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $fp = fopen("demoFormData.txt", "a");
        $savestring = clean_string($name).", ".clean_string($company).", ".clean_string($email).", ".$model.", ".$os.", ".$comments."\n"."-----------------------------------\n";
        fwrite($fp, $savestring);
        fclose($fp);

        echo 'form saved';
    }

对 PHP 很陌生,但对文件读写并不陌生,因此非常感谢任何帮助。

【问题讨论】:

  • 错误代码是什么?
  • @Yani 我不知道如何获得它...我的表单位于一个单独的 PHP 文件中,该文件调用了这个文件。我发现它的方式是那条线是旧的评论和祈祷方法。如何查看错误代码?
  • 我认为 clean_string 函数之前的这个点导致错误 $savestring = .clean_string($name)。一世';;不过需要查看您的错误。
  • @Enijar 很好地摆脱了错误,但它没有向文件写入任何内容......
  • 可能是因为您需要在echo 'form saved'; 之后为您的if(isset($_POST["submit"])){ 加上一个右大括号以读取为echo 'form saved';} @metsales

标签: php


【解决方案1】:

您在代码末尾缺少一个 }。

【讨论】:

    【解决方案2】:

    首先,您需要先定义 $savestring,然后才能向其中添加任何内容(虽然它在大多数情况下都会起作用,但从技术上讲,响应仍然是出乎意料的)。

    $savestring = '';
    

    从性能的角度来看,您调用 clean_string() 函数的次数太多了。 做吧:

    $savestring = cleanstring($name . ", " $company .", " . $email .", " .$model. ", " .$os. ", ".$comments."\n"."-----------------------------------\n");
    

    这样你甚至不需要初始化 $savestring 变量。 要跟踪您的错误,请将您的 error_reporting for development 设置为开启。

    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);
    

    另外,使用 Firefox 或 Chrome 网络监视器查看您从网络服务器获得的响应。

    最后,正如 Kovo 所指出的,假设这是您的完整代码,您在最后缺少一个右括号。

    希望这会有所帮助!

    【讨论】:

    猜你喜欢
    • 2012-05-11
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    相关资源
    最近更新 更多