这个PHP注释系统没有';不起作用


This PHP commenting system doesn't work?

我正在我的网站上对一个帖子系统进行评论,结果是最近的评论被新的评论取代了。我希望所有的评论都留下来,而不仅仅是一条。

<?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()文档。

你很可能想要aa+模式,就像这样:

$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配合良好,但一定要使用mysqliPDO库,不要使用mysql_库:http://php.net/manual/en/mysqlinfo.api.choosing.php