简体   繁体   中英

Comment the same again and again

I am trying to make a comments section, where people can comment stuff on my webpage. All the comments get to a database. Alle that works fine. The only problem I have is, when i have commented some stuff and reload the webpage it comment the same thing again.

Is there a if statement or something to prevent this?

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>

my php code: http://pastebin.com/bQ7c1MPD my inputs: http://pastebin.com/P9uc6Hhz

Use tokens in your HTML form:

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

This require this at the top of the PHP script (before any output):

session_start();

For validation:

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

Full code: PHP | Form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM