【问题标题】:Can't get this simple PHP code to work / guestbook script无法让这个简单的 PHP 代码工作/留言簿脚本
【发布时间】:2016-01-17 19:13:46
【问题描述】:

我找到了一个用于构建您自己的留言簿的 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>

【问题讨论】:

    标签: php wsod


    【解决方案1】:

    问题 1

    您的if 语句中有额外的)s:

    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'];
    

    【讨论】:

    • 你错过了另一个额外的“)”。 if(!empty($user) && !empty($date)**)** && !empty($company) && !empty($note)) {
    • 哎呀 - 忘了从我的代码编辑器中放入该代码的新版本。 :S 谢谢。
    • 嘿,谢谢!这使 wsod 消失了,但是现在当我尝试发布输入时它没有显示。这是因为我添加了
      标签吗?我想要的只是这个脚本来获取输入数据并将其显示在同一页面上,从最新到最旧。我还有其他方法可以解决这个问题吗?
    • @backwrdsman 答案已更新 - 为变量赋值也是一个问题。
    • 是的!我看到了这个并更新了我的文件,现在它可以工作了。抱歉累了大脑 ;-) 最后一个问题:无论如何我可以得到结果并将最新的输入放在上面。现在它显示最旧的在顶部..
    猜你喜欢
    • 2012-05-19
    • 1970-01-01
    • 2017-08-28
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2019-04-13
    • 1970-01-01
    相关资源
    最近更新 更多