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

时间:2013-06-11 11:37:45

标签: php comments

我是一个努力尝试在网站上完成某些功能的菜鸟。不幸的是,我的授权很少,所以我想尽可能简单地创建一个评论框,而不使用数据库或jQuery,JavaScript。

我进行了大量搜索,我认为最简单的方法是创建一个带有注释表单的类似日志的HTML,PHP脚本会在其中附加输入的文本。这是我到目前为止制作的内容:

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

updates.html文件有一个注释框,其中的操作指向包含上述内容的php文件。当然,它不起作用,有一个解析错误,但我不知道如何在那里使用变量(如果这是问题的原因)。我只是想不通怎么做......你有什么建议吗?谢谢!

5 个答案:

答案 0 :(得分:2)

您打开了两次文件。 您不需要回显$ _POST ['update]

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

答案 1 :(得分:2)

不知道你想做什么....

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

答案 2 :(得分:0)

将其置于文件中可能会导致问题,因为您需要某种分隔符,您可以使用base64对其进行编码并在条目末尾追加\ n

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

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

获取条目

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

如果您不想使用Db,则应考虑至少使用 SimpleXml

答案 3 :(得分:0)

必填字段

"; } 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'); } } } ?> 

来自格伦

答案 4 :(得分:0)

此答案适用于将来可能会遇到类似问题的任何人。

在没有数据库的情况下添加注释是不切实际的,这是可行的。以下是你如何去做。

步骤1:创建一个文件并使用.php扩展名保存,如comment.php 步骤2.创建通常的html表单并将表单method =“post”并将表单操作设置为文件名,如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>

` 第3步。 在同一个文件comment.php中编写一个php脚本来处理表单中的数据。请记住将脚本包含在php标记

<?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")); 

?>