如何在没有数据库的PHP中创建评论框?

I'm a noob trying to get some functionalities done on a website. Unfortunately, I have very little authorization, so I would like to create a comment box as simply as possible without the use of databases or jQuery, JavaScript.

I did a lot of searches and I believe the easiest way is to create a log-like HTML with a comment form, to which the PHP script would append the entered text. This is what I've managed to fabricate so far:

$file = "updates.html";
$fh = fopen($file, 'a');
$file = "updates.html";
$fh = fopen($file, 'a');
$comment = echo $_POST["update"] 
";
fwrite($fh, $comment);
fclose($fh);

The updates.html file has a comment box of which action points at a php file with the content above. Of course, it doesn't work, there is a parsing error, but I have no idea how to use the variable there (if that's the cause of the problem). I just can't figure out how to do it... Do you have any suggestions? Thanks!

don't know what you would like to do ....

 <?php
  if(isset($_POST['update'])) {
     // if a request update exists
     $file = "updates.html";
     file_put_contents($file, $_POST['update']."
",FILE_APPEND);
  }
  ?>

You opened your file twice. You don't need to echo your $_POST['update]

<?php 
$file = "updates.html"; 
$fh = fopen($file, 'a'); 
$comment = $_POST["update"] . "
"; 
fwrite($fh, $comment); 
fclose($fh); 
?>

Puting it in file may cause problem because you need some kind of delimiter, you could encode it with base64 and append in the end of an entry

 $input = base64_encode(htmlspecialchars($_POST['update'])); //consider using strip_tags as well to avoid injections

 file_put_contents("updates.html", $input."
");

to get entries use

 $entires = file("updates.html");
 if(count($entries) > 0)
 {
  foreach($entries as $entry)
  {
   echo base64_decode($entry);
  }
 }
 else
 {
   echo 'no entries so far';
 }

You should consider using at least SimpleXml if you don't want to use Db.

Required Field

"; } else { $v_firstname = ""; } if ($lastname=="") { $v_lastname= "Required Field
"; } else { $v_lastname= ""; } if ($password=="") { $v_password= "Required Field
"; } else { $v_password= ""; } if ($password!=$passwordRetype) { $v_passwordRetype= "Password did not match!
"; } else { $v_passwordRetype= ""; } if ($gender=="") { $v_gender= "Required Field
"; } else { $v_gender= ""; } if ($student_id=="") { $v_student_id= "Required Field
"; } else { $v_student_id= ""; } if ($firstname!="" && $lastname!= "" && $password == $passwordRetype && $student_id!= "" && $email!= "" && $gender!= ""){ $checkme=mysql_query("SELECT * FROM members WHERE student_id = '$student_id'") or die(mysql_error()); $checkmyid=mysql_numrows($checkme); if($checkmyid > 0){ header("location:checkid.php"); }else{ mysql_query("INSERT INTO members (firstname, lastname, password,url, gender, student_id, status_id,photo,account_status) VALUES ('$firstname','$lastname','$password','$email','$gender','$student_id','0','default.jpg','0')")or die(mysql_error()); $wewness = mysql_query("SELECT * FROM members WHERE student_id = $student_id")or die(mysql_error()); $getid = mysql_fetch_array($wewness); $_SESSION['member_id'] = $getid['memberid']; $_SESSION['login'] = 'true'; $_SESSION['studentid'] = $student_id; header("location:registerexec.php"); } } } if(isset($_POST['login'])){ $studentid = $_POST['studid']; $pass = $_POST['password']; $query2 = mysql_query("SELECT * FROM members WHERE student_id = '$studentid' AND password = '$pass' ") or die (mysql_error()); while($studid = mysql_fetch_object($query2)) { echo "$studid->member_id"; } $numberOfRows = MYSQL_NUMROWS($query2); if ($numberOfRows == 0) { } else if ($numberOfRows > 0){ $wewness = mysql_query("SELECT * FROM members WHERE student_id = $studentid")or die(mysql_error()); $getid = mysql_fetch_array($wewness); if($getid['account_status']==0){ $_SESSION['login'] = 'maybe'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:registerexec.php'); }elseif($getid['account_status']==2){ $_SESSION['login'] = 'true'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:hometest.php'); }elseif($getid['account_status']==1){ $_SESSION['login'] = 'maybe'; $_SESSION['member_id'] = $getid['member_id']; $_SESSION['studentid'] = $getid['student_id']; header('location:fill.php'); } } } ?> 

by glen

This answer is for anyone who may have a similar question in future.

In as much as adding comment without a database is impractical it is doable. Below is how you can go about it.

Step 1: Create a file and save it with the .php extension like comment.php Step 2. create the usual html form and set the form method = "post" and form action to the name of the file like action = "comment.php"

<h3> Add a comment here </h3>

<form action="comment.php" method="post">
<label for="name">Name:</label>
<input type="text" name="yourname"><br>
<label for="name">Comment:</label> 
<textarea name="comment" id="comment" cols="30" rows="10"></textarea>
<input type="submit" value="submit">
</form>

` Step 3. Write a php script within the same file comment.php to process the data from the form. Remember to enclose the script in the php tag

<?php

$yourname = $_POST['yourname'];
$comment = $_POST['comment'];

// format the comment data into how you want it to be displayed on the page
$data = $yourname . "<br>" . $comment . "<br><br>";

//Open a text file for writing and save it in a variable of your chosen.    
//Remember to use "a" not "w" to indicate write. Using 'w' will overwrite 
// any existing item in the file whenever a new item is written to it.

$myfile = fopen("comment.txt", "a"); 

//write the formatted data into the opened file and close it
fwrite($myfile, $data); 
fclose($myfile);

// Reopen the file for reading, echo the content and close the file
$myfile = fopen("comment.txt", "r");
echo fread($myfile,filesize("comment.txt")); 

?>