0

I found a php script used for building your own guestbook which I tried to make into a simple page for reporting sales. Please take a look at the code because something is wrong, I end up with a WSoD.

I just want some different fields and make these appear on the same page (preferably with auto-date function) when Save is pressed.

<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>
klvsv
  • 11
  • 2

1 Answers1

1

Problem 1

You have extra )s in your if statement:

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

... should be...

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

Problem 2

You also overwrite the same variable a few times, which results in $date and $company being empty:

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

... should be...

$user = $_POST['user'];
$date = $_POST['date'];
$company = $_POST['company'];
$note = $_POST['note'];
Nerdwood
  • 3,947
  • 1
  • 21
  • 20
  • 1
    You missed another extra ")". if(!empty($user) && !empty($date)**)** && !empty($company) && !empty($note)) { – AndrewB Oct 19 '15 at 12:36
  • 1
    Whoops - forgot to put the new version of that code from my code editor. :S Thanks. – Nerdwood Oct 19 '15 at 12:38
  • Hey thanks! This made the wsod disappear but now when I'm trying to post the input it doesn't show. Is this because I've added the
    tag? All I want is this script to take the input data and show it on the same page, newest to oldest. Is there any other way I can go about this?
    – klvsv Oct 19 '15 at 13:03
  • @backwrdsman Answer updated - also a problem with assigning values to variables. – Nerdwood Oct 19 '15 at 13:08
  • Yeah!! I saw this and updated my file and NOW it works. Sorry for tired brain ;-) One last question: is there anyway I can get the results and place the latest input on top. Now it shows the oldest on top.. – klvsv Oct 19 '15 at 13:54
  • You can read all the contents out of the file first, then write the new contents to the file (write, not append) and then append the old contents to the file after that. This would always make sure that the most-recent stuff is first. – Nerdwood Oct 19 '15 at 14:03
  • Thank you so much. Could you perhaps tell me how, or point me to any good tutorial covering this? I'm not that used to PHP. – klvsv Oct 19 '15 at 14:21
  • I found this php code online and customized it from there. – klvsv Oct 19 '15 at 14:22
  • @backwrdsman I think something interactive is best. Try [this Codeacademy tutorial](https://www.codecademy.com/tracks/php) to get started with PHP. If you are interested, look at doing a web course with a good instructor - no better way to learn. :) – Nerdwood Oct 19 '15 at 14:28