PHP-插入新数据时丢失旧数据

时间:2018-10-09 17:32:32

标签: php forms class display

我有两个文件,分别是 index.php ClassProveContakt.php 。我必须在ClassProveContakt.php上构建一个Form数据,在index.php中显示数据和Webformular。每次写入数据(名称,电子邮件和消息)时都会显示该数据,但是如果我写入新数据,则会丢失旧日期并更改为新数据...。

我的问题,如何将所有新旧数据保留在 index.php 上。

ClassProveContakt.php 代码:

<?php

header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Paris');
error_reporting(-1);

class ClassProveContakt
{

  private $Name;
  private $Email;
  private $Message;



       function __construct()
       {

          $this->Name="";
          $this->Email="";
          $this->Message="";

       }

       function Form()
       {

           echo('<table>');


                echo('<label for="name">Name </label>');

                echo('<input type="text" name="Name" value="'.$this->Name.'">');


                echo('<label for="email"> Email </label>');

                echo('<input type="email" value="'.$this->Email.'" name="Email" ');



               echo('<tr>');

                   echo('<td>');

                    echo('<br>');

                      echo('<label> Message: <br><textarea cols="45" rows="6" name="Message">'.$this->Message.'</textarea></label>');

                       echo('<br><br>');

                       echo('<input  type="submit" name="post" value="POST COMMENT" id="comment">');


               echo('</td>');



             echo('</tr>');

        echo('</table>');


   }


   function PostOk()

   {

     if(empty($_POST['Name'])  || 
       empty($_POST['Email']) || 
       empty($_POST['Message'])) 
    {


        echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>";    



        $this->Name=$_POST["Name"];
        $this->Email=$_POST["Email"];
        $this->Message=$_POST["Message"];


    }

    else {

        $name = filter_input( INPUT_POST, 'Name', FILTER_SANITIZE_STRING);
        $email = filter_input(INPUT_POST, 'Email', FILTER_SANITIZE_STRING);
        $message = filter_input(INPUT_POST, 'Message', FILTER_SANITIZE_STRING);

        $datetime = date('m/d/Y h:i:s a', time());

        echo "<br>"

                . "<b>From: </b>" . htmlspecialchars( $name)
                . "<b> at: </b>" . htmlspecialchars( $datetime)
                . "<br><br>" . htmlspecialchars( $message)
                . "<br><hr>";

        }  

  }

} 

?>

index.php 代码:

<?php

 include 'ClassProveContakt.php';
 header('Content-Type: text/html; Charset=utf-8');
 mb_internal_encoding('UTF-8');
 date_default_timezone_set('Europe/Paris');
 error_reporting(-1);

$ProveContackt=new ClassProveContakt();
?> 


<!DOCTYPE html>
<html lang="en_mx">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

   <form name="form" id="form" method="post" action="" >

<?php

     $ProveContackt->form();

     $ProveContackt->PostOk();

?>

</form>
</body>
</html>

截屏我的页面如何显示https://i.imgur.com/xSstxcD.png

2 个答案:

答案 0 :(得分:0)

因此,作为评论,我向您发送了该解决方案。

要恢复数据:

  

$ json_get_data = file_get_contents('myfile.json');

要保存数据:

  

file_put_contents('myfile.json',$ save_data);

ClassProveContakt.php 代码:

 <?php

header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Paris');
error_reporting(-1);

class ClassProveContakt {
    private $Name;
    private $Email;
    private $Message;

    function __construct() {
      $this->Name="";
      $this->Email="";
      $this->Message="";
    }

    function Form() {
     echo('<table>');
     echo('<label for="name">Name </label>');
     echo('<input type="text" name="Name" value="'.$this->Name.'">');
     echo('<label for="email"> Email </label>');
     echo('<input type="email" value="'.$this->Email.'" name="Email" ');
     echo('<tr>');
     echo('<td>');
     echo('<br>');
     echo('<label> Message: <br><textarea cols="45" rows="6" name="Message">'.$this->Message.'</textarea></label>');
     echo('<br><br>');
     echo('<input  type="submit" name="post" value="POST COMMENT" id="comment">');
     echo('</td>');
     echo('</tr>');
     echo('</table>');
   }


  function PostOk() {
       if(empty($_POST['Name']) || empty($_POST['Email']) || empty($_POST['Message'])) {

        echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>";    

        $this->Name=$_POST["Name"];
        $this->Email=$_POST["Email"];
        $this->Message=$_POST["Message"];

      } else {
        $json_get_data = file_get_contents('myfile.json');
        $array_data = (array)json_decode($json_get_data);


        $name = filter_input( INPUT_POST, 'Name', FILTER_SANITIZE_STRING);
        $email = filter_input(INPUT_POST, 'Email', FILTER_SANITIZE_STRING);
        $message = filter_input(INPUT_POST, 'Message', FILTER_SANITIZE_STRING);
        $datetime = date('m/d/Y h:i:s a', time());

        $data = new stdClass();
        $data->name = $name;
        $data->email = $email;
        $data->message = $message;
        $data->datetime = $datetime;

        $array_data[] = $data;


        $save_data = json_encode($array_data);
        file_put_contents('myfile.json', $save_data);

        foreach ($array_data as $key => $value) {

          echo "<br>"
          . "<b>From: </b>" . htmlspecialchars( $value->name)
          . "<b> at: </b>" . htmlspecialchars( $value->datetime)
          . "<br><br>" . htmlspecialchars( $value->message)
          . "<br><hr>";
        }
      }

  }

} 

?>

index.php 代码保持不变:

<?php

 include 'ClassProveContakt.php';
 header('Content-Type: text/html; Charset=utf-8');
 mb_internal_encoding('UTF-8');
 date_default_timezone_set('Europe/Paris');
 error_reporting(-1);

$ProveContackt=new ClassProveContakt();
?> 


<!DOCTYPE html>
<html lang="en_mx">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

   <form name="form" id="form" method="post" action="" >

<?php

     $ProveContackt->form();

     $ProveContackt->PostOk();

?>

</form>
</body>
</html>

答案 1 :(得分:0)

所以,现在我只能使用php代码,而无需Json ...

@雨果,谢谢,你给我指明了方向。...

index.php 保持不变,在 ClassProveContakt.php 中,我必须更改为...

<?php

header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Paris');
error_reporting(-1);

class ClassProveContakt {
 private $Name;
 private $Email;
 private $Message;

function __construct() {
  $this->Name="";
  $this->Email="";
  $this->Message="";
}

function Form() {
 echo('<table>');
 echo('<label for="name">Name </label>');
 echo('<input type="text" name="Name" value="'.$this->Name.'">');
 echo('<label for="email"> Email </label>');
 echo('<input type="email" value="'.$this->Email.'" name="Email" ');
 echo('<tr>');
 echo('<td>');
 echo('<br>');
 echo('<label> Message: <br><textarea cols="45" rows="6" name="Message">'.$this->Message.'</textarea></label>');
 echo('<br><br>');
 echo('<input  type="submit" name="post" value="POST COMMENT" id="comment">');
 echo('</td>');
 echo('</tr>');
 echo('</table>');
}


function PostOk() {

  $file = "test.txt"; 

  $this->Name=$_POST["Name"];
  $this->Email=$_POST["Email"];
  $this->Message=$_POST["Message"]; 

  if(empty($_POST['Name']) || empty($_POST['Email']) || empty($_POST['Message'])) {

    echo "<br>" . "<b>" . "<h3>*** Please enter all required fields ***</h3>" . "</b>";    

  } 

  else 
  {

    $name = filter_input( INPUT_POST, 'Name', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'Email', FILTER_SANITIZE_STRING);
    $message = filter_input(INPUT_POST, 'Message', FILTER_SANITIZE_STRING);
    $datetime = date('m/d/Y h:i:s a', time());



    $data = array("name" => $name, "email" => $email, "message" => $message, "datetime" => $datetime);


   $data = serialize($data);
    file_put_contents($file, $data . "\n", FILE_APPEND|LOCK_EX);    
   }   
   $messages = file($file);

     foreach ($messages as $value) {
       $data = unserialize($value);

          echo "<br>"
            . "<b>From: </b>" . htmlspecialchars( $data["name"])
            . "<b> at: </b>" . htmlspecialchars( $data["datetime"])
            . "<br><br>" . htmlspecialchars( $data["message"])
            . "<br><hr>";
      }   


  }

}



?>

我的screeshot现在如何显示it