【问题标题】:Comment the same again and again一遍又一遍地评论相同
【发布时间】:2015-05-23 14:40:48
【问题描述】:

我正在尝试创建一个 cmets 部分,人们可以在其中评论我网页上的内容。 所有的 cmets 都进入一个数据库。一切正常。 我唯一的问题是,当我评论了一些内容并重新加载网页时,它再次评论了同样的内容。

是否有 if 语句或其他东西来防止这种情况发生?

while ($info = mysql_fetch_array($result)){
    echo '<div style="border-style: solid; border-color: #808080; border-width: thin">
            <div style="width: 1%"><p style="font-size: 10px; margin: 0px">'.$info['Navn'].'</p></div>
            <p>'.$info['Besked'].'</p>
    </div>';
}
 if (isset($_POST['navn']) && isset($_POST['besked']) && isset($_POST['submit'])) {
    $navn2 = $_POST['navn'];
    $besked2 = $_POST['besked'];

    $data = "INSERT INTO `tester`.`davs` (`Navn`, `Besked`) VALUES ('$navn2', '$besked2');";
    $resultalt = mysql_query($data);
    if ($resultalt) {
            echo "$resultat";
    }else{
            echo "$resultat";
    }
    mysql_close();
}
?>

<form action="database.php" method="post" id="commentform">

    <label for="comment_author" class="required">Your name</label>
    <input type="text" name="navn" id="comment_author" value="" tabindex="1" required="required">

    <label for="comment" class="required">Your message</label>
    <textarea name="besked" id="comment" rows="10" tabindex="4"  required="required"></textarea>

    <input type="hidden" name="comment_post_ID" value="1" id="comment_post_ID" />
    <input name="submit" type="submit" value="Submit comment" />

</form>

我的 php 代码: http://pastebin.com/bQ7c1MPD 我的输入: http://pastebin.com/P9uc6Hhz

【问题讨论】:

  • 如果页面被刷新,您可能会发布两次数据......为什么不将您的代码粘贴到您的实际问题中,而不是将我们引导到另一个站点?

标签: php html comments


【解决方案1】:

在 HTML 表单中使用标记:

<input type="hidden" name="token" value="<?=$_SESSION['token'] = uniqid(); ?>" />

这需要在 PHP 脚本的顶部(在任何输出之前):

session_start();

用于验证:

if(isset($_POST['submit']) && $_POST['token'] == $_SESSION['token']))

完整代码:PHP | Form

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 2017-02-15
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2014-02-11
    相关资源
    最近更新 更多