修复刷新php

时间:2016-03-11 08:09:03

标签: php database

我制作了一个简单的网络应用程序,可以将文本添加到数据库,也可以显示它。我有这样的问题,当我添加一个文本(它被添加到数据库并在浏览器中显示)并刷新页面时,在数据库中,即使文本输入为空,也会再次添加文本输入的先前值。 刷新before refresh

after refresh
<?php

        include 'config.php';

        if(isset($_POST['Submit'])) {
                if(strlen(trim($_REQUEST['text'])) > 0) { $conn->query("INSERT INTO Posts (Text_Post,Data) VALUES ('".$_POST['text']."','".date("Y-m-d H:i:s")."')"); 
                $value = '';
              }
                        $result = $conn->query("SELECT * FROM Posts ORDER BY Data DESC");
                        $ul = '<ul>';
                while ($row = $result->fetch_assoc()) {
                        unset($id, $name);
                        $id = $row['ID_Post'];
                        $text = $row['Text_Post'];
                        $data = $row['Data'];
                        $ul .= '<li id="'. $id .'"> '. $text . ' </li>';
                      }
                      $ul .= '</ul>';
                      $lista = $ul;
                $conn->close();
        }
      ?>

      <!DOCTYPE html>
      <html>
      <head>
        <title>anonim</title>
        <link rel="stylesheet" type="text/css" href="style/style.css">
      </head>
      <body>
      <?php 
        include 'config.php';  
        $result = $conn->query("SELECT * FROM Posts ORDER BY Data DESC");
                        $ul = '<ul>';
                while ($row = $result->fetch_assoc()) {
                        unset($id, $name);
                        $id = $row['ID_Post'];
                        $text = $row['Text_Post'];
                        $data = $row['Data'];
                        $ul .= '<li id="'. $id .'"> '. $text . ' </li>';
                      }
                      $ul .= '</ul>';
                      $lista = $ul;
       ?>
        <form name="form" action="" method="post">
          <input type="text" name="text" id="text" />
          <input type="submit" id="Submit" name="Submit" value="Add" /> 
          <div id="lista">
          <?php if(isset($lista)){ echo $lista; } ?>  
          </div>
        </form>
      </body>
      </html>

0 个答案:

没有答案