这个 PHP 评论系统不起作用?
This PHP commenting system doesn't work?
我正在我网站上的 post 系统上发表评论,结果是最近的评论被新评论所取代。我希望保留所有评论,而不仅仅是一个。
<?php
$placement=290;
$comment=fgets($file);
while ($comment!="") {
$placement=$placement+5;
echo'<div style="left:10px; position:relative; top:'.$placement.'px; border:solid; right:10px; height:75px;">';
$comment=fgets($file);
echo "<p>".$comment."</p>";
echo'</div>';
}
$placement2=$placement+5;
if (isset($_SESSION['Username'])) {
echo'<form method="POST" action="">';
echo'<input type="text" columns="10" maxlength="500" style="position:relative; left:50px; width:80%; height:50px; resize:none; top:'.$placement2.'px;" name="comment" placeholder="Leave a comment! (max 500 characters)" >';
}
if (isset($_POST['comment']) or isset($_POST['submit'])) {
if (!empty($_POST['comment'])) {
$file=fopen('Questions/question'.$_GET['qid'],"r");
$name=fgets($file);
$info=fgets($file);
$user=fgets($file);
$n=fgets($file);
$number=1;
while($number<$n) {
$comment=fgets($file);
define("comment".$number,$comment);
echo $number;
$number=$number+1;
}
define("comment".$number,$_POST['comment']);
echo trim(constant("comment1"));
$file=fopen('Questions/question'.$_GET['qid'],"w");
fwrite($file,$name);
fwrite($file,$info);
fwrite($file,$user);
fwrite($file,$n);
$number2=1;
while($number2<$number) {
$number2=$number2+1;
fwrite($file, constant("comment".$number2)."\n");
echo constant("comment".$number2)."\n";
}
}
}
?>
我知道有问题,只是不知道是什么。另外,没有错误。
您正在以 w
模式打开文件,即将指针置于文件开头并覆盖您的内容。
有关详细信息,请参阅 PHP fopen() docs。
您很可能需要 a
或 a+
模式,如下所示:
$file=fopen('Questions/question'.$_GET['qid'],"a+");
注意: 直接基于 $_GET 变量写入文件是不好的做法。您需要清理该变量。 (您应该清理所有用户输入)。在这种情况下,最简单的清理形式如下所示:
// typecast to an integer
$question_id = (int)$_GET['qid'];
// then use the known integer value
$file = fopen('Questions/question' . $question_id,"a+");
最后,对你问题的评论是正确的:如果你使用数据库,你将拥有一个更强大的系统,并且更容易编码。 PHP 与 mysql 相得益彰,但一定要使用 mysqli
或 PDO
库 - 不要使用 mysql_
库:http://php.net/manual/en/mysqlinfo.api.choosing.php
我正在我网站上的 post 系统上发表评论,结果是最近的评论被新评论所取代。我希望保留所有评论,而不仅仅是一个。
<?php
$placement=290;
$comment=fgets($file);
while ($comment!="") {
$placement=$placement+5;
echo'<div style="left:10px; position:relative; top:'.$placement.'px; border:solid; right:10px; height:75px;">';
$comment=fgets($file);
echo "<p>".$comment."</p>";
echo'</div>';
}
$placement2=$placement+5;
if (isset($_SESSION['Username'])) {
echo'<form method="POST" action="">';
echo'<input type="text" columns="10" maxlength="500" style="position:relative; left:50px; width:80%; height:50px; resize:none; top:'.$placement2.'px;" name="comment" placeholder="Leave a comment! (max 500 characters)" >';
}
if (isset($_POST['comment']) or isset($_POST['submit'])) {
if (!empty($_POST['comment'])) {
$file=fopen('Questions/question'.$_GET['qid'],"r");
$name=fgets($file);
$info=fgets($file);
$user=fgets($file);
$n=fgets($file);
$number=1;
while($number<$n) {
$comment=fgets($file);
define("comment".$number,$comment);
echo $number;
$number=$number+1;
}
define("comment".$number,$_POST['comment']);
echo trim(constant("comment1"));
$file=fopen('Questions/question'.$_GET['qid'],"w");
fwrite($file,$name);
fwrite($file,$info);
fwrite($file,$user);
fwrite($file,$n);
$number2=1;
while($number2<$number) {
$number2=$number2+1;
fwrite($file, constant("comment".$number2)."\n");
echo constant("comment".$number2)."\n";
}
}
}
?>
我知道有问题,只是不知道是什么。另外,没有错误。
您正在以 w
模式打开文件,即将指针置于文件开头并覆盖您的内容。
有关详细信息,请参阅 PHP fopen() docs。
您很可能需要 a
或 a+
模式,如下所示:
$file=fopen('Questions/question'.$_GET['qid'],"a+");
注意: 直接基于 $_GET 变量写入文件是不好的做法。您需要清理该变量。 (您应该清理所有用户输入)。在这种情况下,最简单的清理形式如下所示:
// typecast to an integer
$question_id = (int)$_GET['qid'];
// then use the known integer value
$file = fopen('Questions/question' . $question_id,"a+");
最后,对你问题的评论是正确的:如果你使用数据库,你将拥有一个更强大的系统,并且更容易编码。 PHP 与 mysql 相得益彰,但一定要使用 mysqli
或 PDO
库 - 不要使用 mysql_
库:http://php.net/manual/en/mysqlinfo.api.choosing.php