无法将这个简单的PHP代码用于工作/留言板脚本

时间:2015-10-19 12:30:01

标签: php wsod

我发现了一个用于构建自己的留言簿的PHP脚本,我试图将其作为报告销售的简单页面。请看一下代码,因为出了问题,我最终得到了一个WSoD。

我只想要一些不同的字段,并在按下保存时将它们显示在同一页面上(最好带有自动日期功能)。

<html>
<head><title>Reports</title></head>
<body>
<h1>Reports</h1>
<h2>Please fill in the form below and click Save.</h2>

<form action="" method="POST">
<input type="text" name="user" placeholder="Name" />
<br />
<input type="text" name="date" placeholder="Date" />
<br />
<input type="text" name="company" placeholder="Company" />
<br />
<textarea cols="40" rows="5" name="note" placeholder="Report" wrap="virtual"></textarea>
<br />
<input type="submit" name="submit" value="Save" />
</form>
<?php

if (isset($_POST['submit'])){

$user = $_POST['user'];
$user = $_POST['date'];
$user = $_POST['company'];
$note = $_POST['note'];

if(!empty($user) && !empty($date)) && !empty($company)) && !empty($note)) {
$msg = $user . ' <br /> ' . $date . ' <br /> ' . $company . ' <br /> ' . $note;
//will open a file
$fp = fopen("report.txt","a") or die("Can't open file");
//will write to a file
fwrite($fp, $msg."\n");
fclose($fp);
}
}
?>

<h2>Old reports:</h2>
<?php
$file = fopen("report.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  //will return each line with a break
  echo fgets($file). '<br />';
  }
fclose($file);
?> 
</body>
</html>

1 个答案:

答案 0 :(得分:1)

问题1

您的)声明中有额外的if

if(!empty($user) && !empty($date)) && !empty($company)) && !empty($note)) {

......应该......

if(!empty($user) && !empty($date) && !empty($company) && !empty($note)) {

问题2

您还会多次覆盖同一个变量,这会导致$date$company为空:

$user = $_POST['user'];
$user = $_POST['date'];
$user = $_POST['company'];
$note = $_POST['note'];

......应该......

$user = $_POST['user'];
$date = $_POST['date'];
$company = $_POST['company'];
$note = $_POST['note'];